Cross include headers

M

Maart_newbie

Hi all,

Can anyone tell me how I can include one header Form1.h into another
header Form2.h and the other way around without evolving into compile
errors? Both headers contain only one class in which I want to declare
an instance of the class in the other header file.

I assume that through some circulair reference this won't work, but I
don't know how otherwise to be able to declare the instances. The
compile error that I receive is:

1>Proj1.cpp
1>d:\Form2.h(26) : error C2143: syntax error : missing ';' before '^'
1>d:\Form2.h(26) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form2.h(26) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form2.h(35) : error C2065: 'f1' : undeclared identifier
1>d:\Form2.h(35) : error C2061: syntax error : identifier 'Form1'
1>Form2.cpp
1>d:\Form1.h(25) : error C2143: syntax error : missing ';' before '^'
1>d:\Form1.h(25) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form1.h(25) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form1.h(34) : error C2065: 'f2' : undeclared identifier
1>d:\Form1.h(34) : error C2061: syntax error : identifier 'Form2'

These file are part of a VS2006 project and both the classes in the
different files are part of the same namespace.

I was able to succesfully "cross include" the header Form1.h into the
source file belonging to Form2.h (Form2.cpp), but this doesn't give me
the ability to declare the class from Form1.h in Form2.h.

What is the right way to go here, this solution should be simple right?

Thanks in advance, Maart
 
D

David Wilkinson

Maart_newbie said:
Hi all,

Can anyone tell me how I can include one header Form1.h into another
header Form2.h and the other way around without evolving into compile
errors? Both headers contain only one class in which I want to declare
an instance of the class in the other header file.

I assume that through some circulair reference this won't work, but I
don't know how otherwise to be able to declare the instances. The
compile error that I receive is:

1>Proj1.cpp
1>d:\Form2.h(26) : error C2143: syntax error : missing ';' before '^'
1>d:\Form2.h(26) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form2.h(26) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form2.h(35) : error C2065: 'f1' : undeclared identifier
1>d:\Form2.h(35) : error C2061: syntax error : identifier 'Form1'
1>Form2.cpp
1>d:\Form1.h(25) : error C2143: syntax error : missing ';' before '^'
1>d:\Form1.h(25) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form1.h(25) : error C4430: missing type specifier - int assumed.
Note: C++ does not support default-int
1>d:\Form1.h(34) : error C2065: 'f2' : undeclared identifier
1>d:\Form1.h(34) : error C2061: syntax error : identifier 'Form2'

These file are part of a VS2006 project and both the classes in the
different files are part of the same namespace.

I was able to succesfully "cross include" the header Form1.h into the
source file belonging to Form2.h (Form2.cpp), but this doesn't give me
the ability to declare the class from Form1.h in Form2.h.

What is the right way to go here, this solution should be simple right?

Thanks in advance, Maart

Maart:

You need to learn about forward declaration in C++.

// Form2.h

#pragma once

class Form1; // forward declaration

class Form2 // class definition
{
Form1* m_pForm1;
};

// Form2.cpp

#include "Form1.h"
#include "Form2.h"

If Form1 is only used as a pointer in the definition of Form2, the
compiler does not need to see the definition of class Form1 in order to
compute the layout of class Form2.

David Wilkinson
 
S

SvenC

Hi Maart,

Maart_newbie said:
Hi all,


These file are part of a VS2006 project and both the classes in the
different files are part of the same namespace.

I want VS2006, too. Where did you get it ;)
 

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

Top