is this an compiler bug ???

G

Guest

i have two classes...

public class1{
// properties
class2 copy;
}

public class2{
// properties
create copy of class1;
}

i got error in class1 that "class2 is undefined"... however intelli show
shoutcut of class2. if compiler goes step by step; while declaring class1 it
is possible to be class2 undefined but it is too moronish...

how could i solve it ???

ps: if i take class1 bottom of class2 then it says that class1 is undefined
:)
 
C

Carl Daniel [VC++ MVP]

sadun said:
i have two classes...

public class1{
// properties
class2 copy;
}

public class2{
// properties
create copy of class1;
}

i got error in class1 that "class2 is undefined"... however intelli
show shoutcut of class2. if compiler goes step by step; while
declaring class1 it is possible to be class2 undefined but it is too
moronish...

how could i solve it ???

ps: if i take class1 bottom of class2 then it says that class1 is
undefined :)

It's not a compiler bug, but a design bug. You simply can't have classes
that are co-dependent in this way. Since C++ classes are "value types",
when you create a member of "class2" inside "class1", you're actually
embedding a complete instance of class1 (unlike Java or C# when you're
effectively embedding a pointer or reference to an instance of class1).
Naturally if class2 contains a class1, then class1 cannot contain a class2,
since that would result in a data structure of infinite size (since 1
contains 2 contains 1 contains 2 ...).

Now, for what you really wanted:

class class2;

class class1
{
class2* m_c2;
};

class class2
{
class1* m_c1;
};

Sounds like you need to do some reading in an introductory C++ book.

-cd
 
G

Guest

thanks a lot... i have step by step c++.net but i don't like it. actually
its not good..

do you advice something ???
 
C

Carl Daniel [VC++ MVP]

sadun said:
thanks a lot... i have step by step c++.net but i don't like it.
actually its not good..

do you advice something ???

"Accelerated C++" by Andrew Koenig & Barbara Moo is recommended.

"Thinking in C++" by Bruce Eckel is recommened, and available entirely in
HTML online at http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

"The C++ Programming Language" by Bjarne Stroustrup is the seminal text on
C++ (since Bjarne is the inventor of the language).

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top