By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,847 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

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

P: n/a
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)
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
sop3k wrote:
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

--
Artie Gold -- Austin, Texas

Jul 19 '05 #2

P: n/a
sop3k wrote:
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).

--
Jim

To reply by email, remove "link" and change "now.here" to "yahoo"
jfischer_link5809{at}now.here.com
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.