473,402 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

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

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
2 2177
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: shannontbailey | last post by:
I reorganized my VC6 MFC project a bit, and ended up having to rebuild my precompiled header file. I'm not sure if that is relevant or not. I can't get past the following error, however: ...
9
by: ludocluba | last post by:
Hello, here is my problem: I have 3 files: main.c toto.c toto.h: ----------------------------------------------------------- toto.h: #ifndef _toto_h #define _toto_h int var; void test(void);...
1
by: Clocker | last post by:
Hello, I never practiced C++ development in VC++.net before. (only Unix) what does the error: d:\C++\UMath\UAlgebra.cpp(71): fatal error C1010: unexpected end of file while looking for...
5
by: Mikael S. H. | last post by:
Header file compilation I'm coding a small irc bot, and I've noticed that compilation takes very long when I add certain header files (compared to compilation time without). I've tried to find...
0
by: parekh | last post by:
Hi All , I am facing a problem wherein my VB project is not recognizing a change in the argument list of a method ( the method itself being declared and defined in another VC++ project and it...
5
by: =?Utf-8?B?TmlzaGFsIFBhdGVs?= | last post by:
I converted my VC++ 6.0 Project to VC++.NET and during compilation, I get the following error everywhere: Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support...
0
by: anupamak | last post by:
hello, I created a win32 project(precompiled headers, console application). This was created to create a service( for which i used SERVICE_TABLE_ENTRY, SERVICE_STATUS, SERVICE_STATUS_HANDLE,...
4
by: Jonathan Wilson | last post by:
I am using the current platform SDK headers and libraries (including the CRT headers and static CRT libraries). If I #include windows.h and intrin.h in the same source file, the compiler spits out...
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.