473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"syntax error: identifier" instantiating another class within samenamespace?

I've got 2 classes in 2 seperate header files, but within the same
namespace. If I use a line like:
// This code is inside Class2's header file
Class1 *newitem = new Class1(param1, param2);
I get "syntax error: identifier" and "undeclared identifier". Since
they're in the same namespace, and even in the same project, do I need
to do anything special, like put an "#include <theotherfile.h>" in each
header or anything???
Nov 17 '05 #1
4 2700
Stephen Corey wrote:
I've got 2 classes in 2 seperate header files, but within the same
namespace. If I use a line like:
// This code is inside Class2's header file
Class1 *newitem = new Class1(param1, param2);
I get "syntax error: identifier" and "undeclared identifier". Since
they're in the same namespace, and even in the same project, do I need
to do anything special, like put an "#include <theotherfile.h>" in
each header or anything???


Yes.

The compiler only every looks at one file at a time. If you're compiling
x.cpp, the compiler sees x.cpp and everything that it includes (directly or
indirectly) in the order that it's included. Anything that you reference
must have been declared (and possibly defined) before the point of
reference.

Generally speaking, if you have mutually dependent classes, you'll have to
"forward declare" one of the classes. Any decent C++ book will cover the
necessities.

-cd
Nov 17 '05 #2
Carl Daniel [VC++ MVP] wrote:
Stephen Corey wrote:
I've got 2 classes in 2 seperate header files, but within the same
namespace. If I use a line like:
// This code is inside Class2's header file
Class1 *newitem = new Class1(param1, param2);
I get "syntax error: identifier" and "undeclared identifier". Since
they're in the same namespace, and even in the same project, do I need
to do anything special, like put an "#include <theotherfile.h>" in
each header or anything???

Yes.

The compiler only every looks at one file at a time. If you're compiling
x.cpp, the compiler sees x.cpp and everything that it includes (directly or
indirectly) in the order that it's included. Anything that you reference
must have been declared (and possibly defined) before the point of
reference.

Generally speaking, if you have mutually dependent classes, you'll have to
"forward declare" one of the classes. Any decent C++ book will cover the
necessities.

-cd


Thanks for the response!

What if each class references the other one (circular references). How
would you handle that?
Nov 17 '05 #3
Stephen Corey wrote:
Carl Daniel [VC++ MVP] wrote:
Stephen Corey wrote:
I've got 2 classes in 2 seperate header files, but within the same
namespace. If I use a line like:
// This code is inside Class2's header file
Class1 *newitem = new Class1(param1, param2);
I get "syntax error: identifier" and "undeclared identifier". Since
they're in the same namespace, and even in the same project, do I
need to do anything special, like put an "#include <theotherfile.h>" in
each header or anything???

Yes.

The compiler only every looks at one file at a time. If you're
compiling x.cpp, the compiler sees x.cpp and everything that it
includes (directly or indirectly) in the order that it's included.
Anything that you reference must have been declared (and possibly
defined) before the point of reference.

Generally speaking, if you have mutually dependent classes, you'll
have to "forward declare" one of the classes. Any decent C++ book
will cover the necessities.

-cd


Thanks for the response!

What if each class references the other one (circular references). How
would you handle that?


class X;

class Y
{
X* m_X;
};

class X
{
Y* m_y;
};

If you need member functions of the classes to access members of the "other
type", you'll need to define your class implementation outside the class
definition:

struct X
{
int m_i;
void useY();
};

struct Y
{
int m_j;
void useX();
};

void X::useY()
{
Y y;
y.m_j;
}

void Y::useX()
{
X x;
x.m_i;
}

A "forward declaration" (or "incomplete class declaration" in standardese)
allows you to refer to a class in contexts that don't require knowing the
size or interface of a class (such as declaring a pointer or reference
variable or parameter). In order to declare a variable of the class type,
or access any members of the class, a complete class is required.

Between forward declarations and out of line member definitions, you should
be able to handle any sensible mutual dependency.

-cd

Nov 17 '05 #4
Stephen,
Say you have classes X and Y that reference each other. The two headers
would look something like:

X.H:
class Y ;

class X
{
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Charon | last post by:
I see two colons in the middle of what look like method calls here and there. Such as when using PEAR // implement pear db object $this->_oConn =& DB::connect(DSN); I have looked all over...
7
by: news | last post by:
I am pretty new to Java and I just install the JDK1.4.2 03 I am getting an error when I run the class file TestChart.class with the java.exe: Exception in thread "main"...
2
by: Kent | last post by:
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...
8
by: H. S. | last post by:
I am getting this error if I try to compile the file demarcated below. What I am missing here? I am using g++ (GCC) 3.3.5 (Debian 1:3.3.5-8). {tp2}> g++ -ansi -g -Wall tp2.cc -o tp2 tp2.cc: In...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
1
by: rocksoft | last post by:
Hi, i got error "class not registered" when i try to create web setup and deployment project to my web application. i'm used Asp.net and c# to my application, i created web setup and deployment...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: Sunshine | last post by:
The report generated correctly into the rpt but the abover error appeared when the report was exported (through crystal) into excel. Exporting into pdf worked as well. After two days of searching...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.