i can't add a CArray member to a class !!! error c2248 !!!
-
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
-
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
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.
-
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.
-
:( 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
-
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
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();
-
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
"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?
-
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.
hiii this is the header:
#pragma once
#include "ga_dna.h"
#include "afxtempl.h"#define NoOf_cValues 20
#define NoOf_rValues 51class 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
-
"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?
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
-
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();
-
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
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.
-
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.
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