Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Stephen Corey
Guest
 
Posts: n/a
#1: Nov 17 '05
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???

Carl Daniel [VC++ MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

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


Stephen Corey wrote:[color=blue]
> 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???[/color]

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


Stephen Corey
Guest
 
Posts: n/a
#3: Nov 17 '05

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


Carl Daniel [VC++ MVP] wrote:[color=blue]
> Stephen Corey wrote:
>[color=green]
>>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???[/color]
>
>
> 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
>
>[/color]

Thanks for the response!

What if each class references the other one (circular references). How
would you handle that?
Carl Daniel [VC++ MVP]
Guest
 
Posts: n/a
#4: Nov 17 '05

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


Stephen Corey wrote:[color=blue]
> Carl Daniel [VC++ MVP] wrote:[color=green]
>> Stephen Corey wrote:
>>[color=darkred]
>>> 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???[/color]
>>
>>
>> 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
>>
>>[/color]
>
> Thanks for the response!
>
> What if each class references the other one (circular references). How
> would you handle that?[/color]

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



Bob Milton
Guest
 
Posts: n/a
#5: Nov 17 '05

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


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
{
Closed Thread