problem with compilation and headers on MS VC++ (LONG)

S

sop3k

In this code:
*******************FILE: "ClassA.h"***************8

#ifndef _CLASSA_H_
#define _CLASSA_H_

#include "ClassC.h"

class ClassA
{
ClassC& C;
public:
ClassA();
~ClassA();
};
#endif

************************FILE: "ClassAImpl.cpp"******************

#include <iostream>
#include "ClassA.h"

ClassA::ClassA ()
{
std::cout<<"ClassA"<<std::endl;
}
ClassA::~ClassA()
{
std::cout<<"~ClassA"<<std::endl;
}

*****************FILE: "ClassB.h"********************8

#ifndef _CLASSB_H_
#define _CLASSB_H_

#include "ClassA.h"

class ClassB
{
ClassA& A; //line: #1
public:
ClassB(ClassA&); //line: #2
~ClassB();
};
#endif

*******************FILE: "ClassBImpl.cpp"*****************

#include <iostream>
#include "ClassB.h"

ClassB::ClassB(ClassA& a) : A(a)
{
std::cout<<"ClassB"<<std::endl;
}
ClassB::~ClassB()
{
std::cout<<"~ClassB"<<std::endl;
}

***************FILE: "ClassC.h"***************

#ifndef _CLASSC_H_
#define _CLASSC_H_

#include "ClassA.h"
#include "ClassB.h"

class ClassC
{
ClassA& A; //line : #3
ClassB& B;
public:
ClassC(ClassA&, ClassB&); //line: #4
~ClassC();
};
#endif

**********************FILE: "ClassC.cpp"********************

#include <iostream>
#include "ClassC.h"

ClassC::ClassC(ClassA& a, ClassB& b) : A(a), B(b)
{
std::cout<<"ClassC"<<std::endl;
}
ClassC::~ClassC()
{
std::cout<<"~ClassC"<<std::endl;
}

***********************FILE: "MainClass.h"**************************
#ifndef _MAIN_H_
#define _MAIN_H_

#include "ClassA.h"
#include "ClassB.h"
#include "ClassC.h"

class MainClass
{
ClassA* A;
ClassB* B;
ClassC* C;
public:
MainClass();
~MainClass();
};
#endif

******************FILE: "MainClass.cpp"*************************
#include <iostream>
#include "MainHeader.h"


MainClass::MainClass()
{
std::cout<<"MainClass"<<std::endl;
A = new ClassA;
B = new ClassB(*A);
C = new ClassC(*A,*B);
}

MainClass::~MainClass()
{
std::cout<<"~MainClass"<<std::endl;
delete A;
delete B;
delete C;
}

******************FILE:"Main.cpp"******************************
#include "MainClass.h"

int main()
{
MainClass MainObj;
return 0;
}

This is an expample code base on my real program. When I want to
compile this with MS VC++ 6.0 I get an errors such this:

error C2143: syntax error : missing ';' before '&' (line: #1)
ClassA' : missing storage-class or type specifiers (line: #1)
'A' : missing storage-class or type specifiers (line: #1)
unexpected 'class ClassB (' (line: #2)
unexpected token(s) preceding ';' (line: #2)
syntax error : missing ';' before '&' (line: #3)
ClassA' : missing storage-class or type specifiers (line: #3)
'A' : missing storage-class or type specifiers (line: #3)
unexpected 'class ClassC (' (line: #4)
unexpected token(s) preceding ';' (line: #4)
 
A

Artie Gold

sop3k said:
In this code:
*******************FILE: "ClassA.h"***************8

#ifndef _CLASSA_H_
#define _CLASSA_H_

#include "ClassC.h"

class ClassA
{
ClassC& C;
public:
ClassA();
~ClassA();
};
#endif

************************FILE: "ClassAImpl.cpp"******************

#include <iostream>
#include "ClassA.h"

ClassA::ClassA ()
{
std::cout<<"ClassA"<<std::endl;
}
ClassA::~ClassA()
{
std::cout<<"~ClassA"<<std::endl;
}

*****************FILE: "ClassB.h"********************8

#ifndef _CLASSB_H_
#define _CLASSB_H_

#include "ClassA.h"

class ClassB
{
ClassA& A; //line: #1
public:
ClassB(ClassA&); //line: #2
~ClassB();
};
#endif

*******************FILE: "ClassBImpl.cpp"*****************

#include <iostream>
#include "ClassB.h"

ClassB::ClassB(ClassA& a) : A(a)
{
std::cout<<"ClassB"<<std::endl;
}
ClassB::~ClassB()
{
std::cout<<"~ClassB"<<std::endl;
}

***************FILE: "ClassC.h"***************

#ifndef _CLASSC_H_
#define _CLASSC_H_

#include "ClassA.h"
#include "ClassB.h"

class ClassC
{
ClassA& A; //line : #3
ClassB& B;
public:
ClassC(ClassA&, ClassB&); //line: #4
~ClassC();
};
#endif

**********************FILE: "ClassC.cpp"********************

#include <iostream>
#include "ClassC.h"

ClassC::ClassC(ClassA& a, ClassB& b) : A(a), B(b)
{
std::cout<<"ClassC"<<std::endl;
}
ClassC::~ClassC()
{
std::cout<<"~ClassC"<<std::endl;
}

***********************FILE: "MainClass.h"**************************
#ifndef _MAIN_H_
#define _MAIN_H_

#include "ClassA.h"
#include "ClassB.h"
#include "ClassC.h"

class MainClass
{
ClassA* A;
ClassB* B;
ClassC* C;
public:
MainClass();
~MainClass();
};
#endif

******************FILE: "MainClass.cpp"*************************
#include <iostream>
#include "MainHeader.h"


MainClass::MainClass()
{
std::cout<<"MainClass"<<std::endl;
A = new ClassA;
B = new ClassB(*A);
C = new ClassC(*A,*B);
}

MainClass::~MainClass()
{
std::cout<<"~MainClass"<<std::endl;
delete A;
delete B;
delete C;
}

******************FILE:"Main.cpp"******************************
#include "MainClass.h"

int main()
{
MainClass MainObj;
return 0;
}

This is an expample code base on my real program. When I want to
compile this with MS VC++ 6.0 I get an errors such this:

error C2143: syntax error : missing ';' before '&' (line: #1)
ClassA' : missing storage-class or type specifiers (line: #1)
'A' : missing storage-class or type specifiers (line: #1)
unexpected 'class ClassB (' (line: #2)
unexpected token(s) preceding ';' (line: #2)
syntax error : missing ';' before '&' (line: #3)
ClassA' : missing storage-class or type specifiers (line: #3)
'A' : missing storage-class or type specifiers (line: #3)
unexpected 'class ClassC (' (line: #4)
unexpected token(s) preceding ';' (line: #4)

In order to simplify even further, consider the following translation unit:

#include "ClassA.h"
#include "ClassB.h"
#include "ClassC.h"

If you go through what this the compiler would see as a result of
preprocessing, the first thing is the declaration of ClassB (at which
time it has not yet seen the declaration of ClassA.

Your other errors arise similarly.

HTH,
--ag
 
J

Jim Fischer

sop3k said:
In this code:
*******************FILE: "ClassA.h"***************8

#ifndef _CLASSA_H_
#define _CLASSA_H_

Names that start with an underscore '_' (e.g., _CLASSA_H) are reserved
for the implementation's use; you must not declare such names in your
own programs.
#include "ClassC.h"

class ClassA
{
ClassC& C;
public:
ClassA();
~ClassA();
};
#endif

************************FILE: "ClassAImpl.cpp"******************

#include <iostream>
#include "ClassA.h"

ClassA::ClassA ()
{
std::cout<<"ClassA"<<std::endl;
}
ClassA::~ClassA()
{
std::cout<<"~ClassA"<<std::endl;
}
[snip]

In C++, references *MUST* be initialized:

T & r1; // ERROR
T tobj;
T & r2 = tobj; // Correct

Note that:

* When you declare a reference, this DOES NOT create an object of the
specified type.

* In C++, there is no such beast as a null reference. IOW, a C++
reference cannot refer to "nothing".

* The C++ reference mechanism lets you logically bind an identifier to
an existing object or function. For example: after a program explicitly
instantiates an object, it can use C++'s reference mechanism to bind an
identifier to that object. After the identifier is bound to the object,
the program can use the identifier to refer to the object.


In your code sample, ClassA contains a data member that is a reference
to a ClassC instance, but your ClassA constructor does not initialize
this reference (via a ctor initializer list). So the compiler complains
that the code is ill formed.

And finally, your program has circular dependency problems -- e.g.,
ClassA has a data member that is a reference to a ClassC instance, and
ClassC has a data member that is a reference to a ClassA instance. So if
you want to construct a ClassA object, you must already have a ClassC
object with which to initialize the reference data member 'C' in the
ClassA object. But in order to create this ClassC object, you must
already have a ClassA object with which to initialize the reference data
member 'A' within the ClassC object. IOW, you cannot create either
ClassA or ClassC objects because of circular dependency problems (i.e.,
the design of these two classes is flawed).
 

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
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top