Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. i can't add a CArray member to a class !!! error c2248 !!!

i can't add a CArray member to a class !!! error c2248 !!!

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpvisual-studioquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    aliusam
    wrote on last edited by
    #1

    my program works fine, untill i tried to add some feature. i added a " CArray AA " to my class in the header file and the code no longer compiles, i did not refrence the variable yet and it gives me a function related error, here is the error: 1>D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator =' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(524) : see declaration of 'CObject' 1> This diagnostic occurred in the compiler generated function 'CArray &CArray::operator =(const CArray & )' 1> with 1> [ 1> TYPE=CString 1> ] if i comment out the line " CArray AA " the code compiles with no problem, i am using CArray in other parts in the program but not as members, they are created inside functions and they work fine why is the error and what can i do about it?? i want to read a list from a text file that a user saves Ali Usam AlCherchefchi modified on Saturday, December 6, 2008 1:16 PM

    J Richard Andrew x64R S 3 Replies Last reply
    0
    • A aliusam

      my program works fine, untill i tried to add some feature. i added a " CArray AA " to my class in the header file and the code no longer compiles, i did not refrence the variable yet and it gives me a function related error, here is the error: 1>D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator =' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(524) : see declaration of 'CObject' 1> This diagnostic occurred in the compiler generated function 'CArray &CArray::operator =(const CArray & )' 1> with 1> [ 1> TYPE=CString 1> ] if i comment out the line " CArray AA " the code compiles with no problem, i am using CArray in other parts in the program but not as members, they are created inside functions and they work fine why is the error and what can i do about it?? i want to read a list from a text file that a user saves Ali Usam AlCherchefchi modified on Saturday, December 6, 2008 1:16 PM

      J Offline
      J Offline
      Jijo Raj
      wrote on last edited by
      #2

      Might be due to the parameters for CArray constructor. Try this one.

      CArray<CString,CString&> AA;

      I hope that will fix the error. Well, if you just want to keep a list of string, you could use CStringArray as well. Regards, Jijo.

      _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

      A 1 Reply Last reply
      0
      • J Jijo Raj

        Might be due to the parameters for CArray constructor. Try this one.

        CArray<CString,CString&> AA;

        I hope that will fix the error. Well, if you just want to keep a list of string, you could use CStringArray as well. Regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        A Offline
        A Offline
        aliusam
        wrote on last edited by
        #3

        :( thanks for the reply but that did not help i tried

        CArray<CString,CString&> AA;

        and

        CStringArray AA;

        same error in both cases, it is looking for the operator =, but i did not use the variable yet:confused:

        Ali Usam AlCherchefchi

        J 1 Reply Last reply
        0
        • A aliusam

          :( thanks for the reply but that did not help i tried

          CArray<CString,CString&> AA;

          and

          CStringArray AA;

          same error in both cases, it is looking for the operator =, but i did not use the variable yet:confused:

          Ali Usam AlCherchefchi

          J Offline
          J Offline
          Jijo Raj
          wrote on last edited by
          #4

          Could you please post the code snippet of the whole class? So that i can have a look at it. Regards, Jijo.

          _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

          A 1 Reply Last reply
          0
          • A aliusam

            my program works fine, untill i tried to add some feature. i added a " CArray AA " to my class in the header file and the code no longer compiles, i did not refrence the variable yet and it gives me a function related error, here is the error: 1>D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator =' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(524) : see declaration of 'CObject' 1> This diagnostic occurred in the compiler generated function 'CArray &CArray::operator =(const CArray & )' 1> with 1> [ 1> TYPE=CString 1> ] if i comment out the line " CArray AA " the code compiles with no problem, i am using CArray in other parts in the program but not as members, they are created inside functions and they work fine why is the error and what can i do about it?? i want to read a list from a text file that a user saves Ali Usam AlCherchefchi modified on Saturday, December 6, 2008 1:16 PM

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            When you put it as a member in the header file, it gets instantiated as soon as the class is instantiated. You don't need to reference it to cause it to be called. Therefore, you need to call the constructor in the header file, like so: CArray<cstring,cstring> AA();

            A 1 Reply Last reply
            0
            • A aliusam

              my program works fine, untill i tried to add some feature. i added a " CArray AA " to my class in the header file and the code no longer compiles, i did not refrence the variable yet and it gives me a function related error, here is the error: 1>D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afxtempl.h(272) : error C2248: 'CObject::operator =' : cannot access private member declared in class 'CObject' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(554) : see declaration of 'CObject::operator =' 1> D:\Program Files\Microsoft Visual Studio 8\VC\atlmfc\include\afx.h(524) : see declaration of 'CObject' 1> This diagnostic occurred in the compiler generated function 'CArray &CArray::operator =(const CArray & )' 1> with 1> [ 1> TYPE=CString 1> ] if i comment out the line " CArray AA " the code compiles with no problem, i am using CArray in other parts in the program but not as members, they are created inside functions and they work fine why is the error and what can i do about it?? i want to read a list from a text file that a user saves Ali Usam AlCherchefchi modified on Saturday, December 6, 2008 1:16 PM

              S Offline
              S Offline
              Snorri Kristjansson
              wrote on last edited by
              #6

              "TYPE" needs to overload the '=' operator to work because when an item is added to the collection (CArray) a new instance of "TYPE" is created and the 'source' is copied into the new instance. So for this to work you need to create a new class and overload the '=' operator to copy all data members. BTW why don't you use CStringArray?

              A 1 Reply Last reply
              0
              • J Jijo Raj

                Could you please post the code snippet of the whole class? So that i can have a look at it. Regards, Jijo.

                _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                A Offline
                A Offline
                aliusam
                wrote on last edited by
                #7

                hiii this is the header:

                #pragma once
                #include "ga_dna.h"
                #include "afxtempl.h"

                #define NoOf_cValues 20
                #define NoOf_rValues 51

                class GA_Individual
                {
                public:
                GA_Individual(void);
                public:
                virtual ~GA_Individual(void);
                public:
                GA_DNA dna1;
                .
                .
                .
                .
                .
                // constructors
                GA_Individual(GA_DNA* pDNA);

                static unsigned short c\[NoOf\_cValues\];
                static unsigned short r\[NoOf\_rValues\];
                

                public:
                // always call this function befor using the class to initialize it
                static bool initIndiv(void);

                CArray <CString> opamp;        //tried "	CArray <CString,CString&> opamp;
                

                " and the same
                };

                the .cpp is like:

                #include "StdAfx.h"
                #include "GA_Individual.h"
                //#define _CRT_RAND_S // required to use rand_s() // defined in the compiler comand line
                #include "stdlib.h"
                #include "windows.h"

                #include <tchar.h>
                #include <math.h>

                unsigned short GA_Individual::c[NoOf_cValues];
                unsigned short GA_Individual::r[NoOf_rValues];

                GA_Individual::GA_Individual(void)
                : Age(0)
                {
                CArray <CString> opamp(void); // i tried this line but it did not solve a thing
                }

                GA_Individual::~GA_Individual(void)
                {
                }

                GA_Individual::GA_Individual(GA_DNA* pDNA)
                {
                ......
                }

                bool GA_Individual::initIndiv(void)
                {
                ......
                }
                .
                ....... other function definitions here
                .

                i noticed a thing, if i make the array a ststic ( static CArray opamp; ) member the code compiles, i can use it as a static with few axtra lines, but i am thinking what if using it as static was not an option, one should be able to add any type to the class, right, even if it did not have the operator '=' ???

                Ali Usam AlCherchefchi

                1 Reply Last reply
                0
                • S Snorri Kristjansson

                  "TYPE" needs to overload the '=' operator to work because when an item is added to the collection (CArray) a new instance of "TYPE" is created and the 'source' is copied into the new instance. So for this to work you need to create a new class and overload the '=' operator to copy all data members. BTW why don't you use CStringArray?

                  A Offline
                  A Offline
                  aliusam
                  wrote on last edited by
                  #8

                  you mean to drive a class from "TYPE" and overload the '=' operator?? the CString already have one, or you mean to drive from the CArray and overload the '='??? i think this will work but not necessary, if the code will use the '=' operator each time i add an item this will be a big performance problem as the array may be long, 1000 or 10000, not sure yet. now i think the problem is the class could not construct the array, but adding it in the constructor did not help!!!!

                  Ali Usam AlCherchefchi

                  S 1 Reply Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    When you put it as a member in the header file, it gets instantiated as soon as the class is instantiated. You don't need to reference it to cause it to be called. Therefore, you need to call the constructor in the header file, like so: CArray<cstring,cstring> AA();

                    A Offline
                    A Offline
                    aliusam
                    wrote on last edited by
                    #9

                    i tried but the same error, i also tried the initialization list but the same.

                    Ali Usam AlCherchefchi

                    1 Reply Last reply
                    0
                    • A aliusam

                      you mean to drive a class from "TYPE" and overload the '=' operator?? the CString already have one, or you mean to drive from the CArray and overload the '='??? i think this will work but not necessary, if the code will use the '=' operator each time i add an item this will be a big performance problem as the array may be long, 1000 or 10000, not sure yet. now i think the problem is the class could not construct the array, but adding it in the constructor did not help!!!!

                      Ali Usam AlCherchefchi

                      S Offline
                      S Offline
                      Snorri Kristjansson
                      wrote on last edited by
                      #10

                      No I don't mean that. The error message you get is related to the '=' operator. Probably the class you declare the CArray in already has one? Or at least you are using '=' somewhere in your code for an instance of that class. You need to copy the AA member in the '=' operator definition yourself. E.g. AA.Copy( source.AA ); the CArray template does not have an '=' operator.

                      A 1 Reply Last reply
                      0
                      • S Snorri Kristjansson

                        No I don't mean that. The error message you get is related to the '=' operator. Probably the class you declare the CArray in already has one? Or at least you are using '=' somewhere in your code for an instance of that class. You need to copy the AA member in the '=' operator definition yourself. E.g. AA.Copy( source.AA ); the CArray template does not have an '=' operator.

                        A Offline
                        A Offline
                        aliusam
                        wrote on last edited by
                        #11

                        hiiii thank you all for the help, now it is working really great. but let me make things clear so if someone had the same problem can see where the error, cause the message you get from the compiler is not even close from the problem. let say you have a class: class BB { public BB(); virtual ~BB(); CArray AA; // or even a CAtringArray BB &operator=(const BB &otherBB) // without this operator you cant add a CArray or CStringArray }

                        Snorri wrote:

                        Probably the class you declare the CArray in already has one? Or at least you are using '=' somewhere in your code for an instance of that class

                        when i read that i new the problem, it is in my class, see no class has no operator'=' (that if you used it in the program), so if you dont define one, the compiler will, and it will do it by calling each member's '=' operator, and if the operator is not existed then it stops and gives an error, evry one knows that but the error explanation dont even mentions it, and when i searched i didnot find an answer, so one can be drifted away. thanks all again regards

                        Ali Usam AlCherchefchi

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups