HELP!!! - Files Linking

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have two files x.h and y.h which define 'class X' and 'class Y'
X has data member of type Y => there is - #include "y.h" in the file x.
Y has data member of type X => there is - #include "x.h" in the file y.

why the compiler doesn't let me do it ??????????!!!!!!!!!
what am i missing ?
thank
 
ya said:
I have two files x.h and y.h which define 'class X' and
'class Y' : X has data member of type Y => there is - #include
"y.h" in the file x.h Y has data member of type X => there is -
#include "x.h" in the file y.h

why the compiler doesn't let me do it ??????????!!!!!!!!!!
what am i missing ??
thanks

The fact that it's fundamentally impossible, perhaps? You have a
recursively defined class that contains an instance of itself. This isn't
possible in C++. It's possible in languages like C# and Java where classes
are always held by reference (i.e. pointer in disguise). In C++ classes are
inherently value types so it's impossible to imbed an instance of a class in
another instance of that same type, either directly or indirectly, but it is
possible to embed a pointer (or reference) to a class in an instance of that
same class.

For example:

// X.h

class Y;

class X
{
Y* m_py;
};

// Y.h
#include "X.h"

class Y
{
X m_x;
};

-cd
 
thank you for the answer , but i still have the problem :
i meant from the begining that i hold reference ,and as you explained it should work,
but it doesn't.
take a look at this code : (it doesn't work)

*********************************
// x.h
#ifndef X_H_
#define X_H_

#include "y.h"

class Y; // I don't understand why i should put a forward declaration

class X
{
public:
X(Y& _my) : my(_my) {my.print();}
private:
Y& my;
}

#endif
*********************************

// y.h
#ifndef Y_H_
#define Y_H_

#include "x.h"

class Y
{
Y() {} // supose that Y don't have X (this is not my problem)

void print() { cout << "Y print" << endl; }
}

#endif
*********************************
 
ya said:
thank you for the answer , but i still have the problem :
i meant from the begining that i hold reference ,and as you explained
it should work, but it doesn't.
take a look at this code : (it doesn't work)

*********************************
// x.h
#ifndef X_H_
#define X_H_

#include "y.h"

^^^^^^ remove this line
 
I am sorry ,but still the same proble
when i add file x.cpp and compile it i get this error messages

*************************************
x.cp
x.h(10) : error C2027: use of undefined type 'Y
x.h(4) : see declaration of 'Y
x.h(10) : error C2228: left of '.print' must have class/struct/union typ
*************************************
the x.cpp contain only one line
#include "x.h

i really don't understand that
in case no cpp file in the project - it is ok
but if i add main or x.cpp the errors comes
 
ya said:
I am sorry ,but still the same problem
when i add file x.cpp and compile it i get this error messages :

**************************************
x.cpp
x.h(10) : error C2027: use of undefined type 'Y'
x.h(4) : see declaration of 'Y'
x.h(10) : error C2228: left of '.print' must have class/struct/union
type
**************************************
the x.cpp contain only one line :
#include "x.h"

i really don't understand that.
in case no cpp file in the project - it is ok.
but if i add main or x.cpp the errors comes.

Get a better C++ book, I guess. Or post a more complete example of what
you're trying to do.

-cd
 
hello again,
the exact problem is :
'class X' wants to hold a reference to 'class Y' ,
which have a data member of type X.(or a list of type X)

in simple words - X wants to know about its owner.


here are the 3 files that i am using (copy-paste) :
*********************
// main.cpp
#include <iostream.h>
#include "y.h"

int main()
{
Y y;
return 0;
}
*********************
// y.h
#ifndef Y_H_
#define Y_H_

#include "x.h"

class Y
{
public:
Y() { mx(*this); }
void print() { cout << "Y print" << endl; }
private:
X mx;
};

#endif
*********************
#ifndef X_H_
#define X_H_

class Y;

class X
{

public:
X(Y& _my) : my(_my) { my.print(); }

private:
Y& my;
};

#endif
*********************
here is the compilation errors that i receive :
main.cpp
x.h(10) : error C2027: use of undefined type 'Y'
x.h(4) : see declaration of 'Y'
x.h(10) : error C2228: left of '.print' must have class/struct/union type
Error executing cl.exe.
*********************
thanks and sorry for troubling you,
if i had you mail i would send you this project,
but u can use copy-paste from here if u want
thanks
 
it works thanks to u
all thanks to the forward decleration and the emition of the #include "y.h
and in the cpp file i could put this #include "y.h
thanks you saved me
 
Back
Top