|
-
November 27th, 2002, 03:27 PM
#1
how to let class obs as private member of each other?
Code:
#include <iostream.h>
class CData2;
class CData
{
private:
int m_x;
int m_y;
CData2 cd2;
public:
CData();
CData(int a, int b);
int getc1();
};
class CData2
{
private:
int m_u;
int m_v;
CData cd;
public:
CData2(int u, int v);
int get1();
int get2();
void init(CData& ad1);
};
above code have compilation error:
E:\jzhu\vc\t1\tclass.cpp(8) : error C2079: 'cd2' uses undefined class 'CData2'
How to fix this problem?
Thanks
-
November 27th, 2002, 03:39 PM
#2
Hmmm...
I pasted the code into one of my projects. it compiled without error. It certainly looks valid.
-
November 27th, 2002, 03:42 PM
#3
It can't compile since you're having 'circular reference'. making the class have infinite size if you would actually want to create such an object.
CD1 contains a CD2 which contains a CD1 which contains a CD2 which contains a CD1 which contains a CD2 which contains a CD1 which contains a CD2 which contains a CD1 which contains a CD2 which contains a CD1 which contains a CD2 which contains a CD1 which contains a CD2 ...
If you need to have circular reference, you can only store either references or pointers to the other class, but not the actual classes themselves.
-
November 27th, 2002, 03:45 PM
#4
Originally posted by billwilson3
I pasted the code into one of my projects. it compiled without error. It certainly looks valid.
Not possible.
You either didn't try, didn't compile, it didn't get compiled (code in #ifdef ???) or you altered to code to have a pointer or reference to CData2 instead of an object of CData2.
-
November 27th, 2002, 04:37 PM
#5
Actually I did try it. I altered nothing. It did compile. There are no #ifdefs involved as I put it at the top of the file.
Since you clearly won't believe me. Look at the attachment. MessageArrived is the file I pasted the code into. I don't know why it compiles, because I agree with your analysys. It looks like it would cause an infinite loop of contstructor calling, if nothing else!
Be that as it may, it DID compile!!
-
November 27th, 2002, 04:45 PM
#6
The attachment seems to have gotten lost from the previous post.
-
November 27th, 2002, 04:55 PM
#7
The more I thought about, the more convinced I became that this code SHOULD NOT compile. I built an empty project and inserted the code. It does indeed FAIL to compile.
I still don't know why it compiles in the project I tested it in.
I've attached a .bmp to two posts, showing what I did. But, it doesn't seem to show up.
Last edited by billwilson3; November 27th, 2002 at 05:24 PM.
-
November 27th, 2002, 07:49 PM
#8
re: O'Reubens
I understand the problem quite clearly, but i am completely baffled at the error returned. Although I know such things are outside the standard, why would VC++ return such an unrelated error message instead of some type of "circular inclusion" error??
Here's my assumption right now: is the compiler attempting to instantiate CData, and notices it first needs to instantiate CData2. It then tries to instantiate CData2, but finds it needs to instantiate CData. Now, it actually shows some intelligence and realizes that this class is already in the process of being instantiated, so it throws an error for CData2. But the error message is so obscure (and not quite correct), and it seems it would have to know the problem of circular instantiation here to catch the logic or the compiler would just run around in circles until it exhausted system resources or some recursiveness depth check or something...
Maybe I just expect too much here, but CData2 really does have a definition...
*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
"It's hard to believe in something you don't understand." -- the sidhi X-files episode
galathaea: prankster, fablist, magician, liar
-
November 28th, 2002, 02:41 PM
#9
Actually, the error message is correct. and very much to the point.
In order to embed a class as member into another class, the compiler expects to have processed the full definition of the class you want to embed.
This is not the case here. At the time you define class CData, you have only told the compiler that "assume there is a class named CData2 at this point, but I'll tell you what's in it later".
The class forward definition is enough to allow for pointers and references to the class, but not the class itself.
Thus, at the time the compiler tries to embed CData2 in CData, it correctly informs you that "It doesn't know what CData2 looks like (yet)".
-
January 14th, 2004, 03:34 AM
#10
It's a pretty old topic but it covers my problem. I can't seem to successfully implement the following solution.
Originally posted by OReubens
If you need to have circular reference, you can only store either references or pointers to the other class, but not the actual classes themselves.
I have the following classes/definitions:
Code:
#include "MyTreeCtrl.h"
class CGroups
{
public:
void updateTree(CMyTreeCtrl *lp_tree);
}
// and in a different file:
#include "Groups.h"
class CMyTreeCtrl : public CTreeCtrl
{
protected:
CGroups* getGroupsReference(void);
}
This results in circular reference compiler errors. So what am I doing wrong?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|