Forward declaration and nested classes ..

S

Susan Baker

Hi,

I have the following code which refuses to compile:

Snippet from header file:

class C ;
class A ;
class L ;
class T ;


class TC {

private:
void *ct ;
std::vector < PB > dat ; /* One or more */
std::list < C > pat ;
std::list < A > nts ;
std::list < L > ls ;
std::list < T > tl ;
int bIdx ; /* base index */
myEnum sType ;
bool spread ;

protected:
class BPat {

public:
BPat() ;
~BPat() ;

virtual long gSD( void ) = 0 ;
virtual double gSV( void ) = 0 ;
//<snip> ...
};

public:

class TL : protected BPat {
private:
int sd ;
int ed ;
double sv ;
double ev ;
A text ; // <- Compiler complains here !!


Error message:
error C2079: 'TC::TL::text' uses undefined class 'A'


I used class A earlier on in the declaration of class TC, and that was
fine, why is the compiler suddenly (i.e. several lines further down),
complaining that A is an unidentified class?

Any pointers much apprreciated - mtia

S
 
V

Victor Bazarov

Susan said:
I have the following code which refuses to compile:

Snippet from header file:

class A ;

#include said:
class TC {
std::list < A > nts ;
> protected:
> class BPat {};
public:
class TL : protected BPat {
A text ; // <- Compiler complains here !!

<rant>
See how I managed to shorten your code? Isn't it easier to read now?
Why couldn't you do that? said:
Error message:
error C2079: 'TC::TL::text' uses undefined class 'A'


I used class A earlier on in the declaration of class TC, and that was
fine, why is the compiler suddenly (i.e. several lines further down),
complaining that A is an unidentified class?

The declaration of the 'nts' member doesn't need to know what the size
of 'A' is. The size of a list<A> does not depend on it. It will only
need to know the size of the list's stored object when you begin to add
to the list because then the compiler will try to use 'A's constructor.
OTOH when you declare 'TC::TL::text', the compiler needs to know the size
of 'A' to calculate the size of 'TC::TL'.

V
 

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,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top