Two classes dependent on each other, generating "syntax error: identifier 'NAME' "

K

Kent

Hi!

I think i know whats the problem but not how to solve it.

I have two classes, named A and B.

The A class has a list<B*>

And the B class has an pure virtual method declared as: void methodname(A*
Ap)

When compiling this, the compiler complains that the A class isn´t compiled
when trying to compile the B class. They are dependent on eachother, and can
not compile.

How can i solve this? I have looked through my two C++ books as good as i
could and googled for the answer. Any help would be greatly appreciated!
Btw, sorry for any typing or grammatical errors, english isn´t my native
language.

Best regards and Merry Christmas, Kent
 
G

Gianni Mariani

Kent said:
Hi!

I think i know whats the problem but not how to solve it.

I have two classes, named A and B.

The A class has a list<B*>

And the B class has an pure virtual method declared as: void methodname(A*
Ap)

When compiling this, the compiler complains that the A class isn´t compiled
when trying to compile the B class. They are dependent on eachother, and can
not compile.

How can i solve this? I have looked through my two C++ books as good as i
could and googled for the answer. Any help would be greatly appreciated!
Btw, sorry for any typing or grammatical errors, english isn´t my native
language.

Best regards and Merry Christmas, Kent


you mean like this ?

struct A;

struct B
{
virtual void func( A*);
};

struct A
{
int stuff;
};

virtual void B::func( A* p_a )
{
p_a->stuff = 2;
}

// typo warnings ... I did't check the above code.
 
D

Derek

Kent said:
Hi!

I think i know whats the problem but not how to solve it.

I have two classes, named A and B.

The A class has a list<B*>

And the B class has an pure virtual method declared as: void
Amethodname(A* p)

When compiling this, the compiler complains that the A class
isn´t compiled when trying to compile the B class. They are
dependent on eachother, and can not compile.

How can i solve this? I have looked through my two C++ books
as good as i could and googled for the answer. Any help
would be greatly appreciated! Btw, sorry for any typing or
grammatical errors, english isn´t my native language.

Best regards and Merry Christmas, Kent

You wan to read up on forward declarations. Here's a page I found on
Google that might help you (I'm sure there are lots of others):

http://www.adp-gmbh.ch/cpp/forward_decl.html
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top