Visual C++ and include order.

H

Holden

Hello,
I am using Visual C++ 6.0 with Service Pack 5 installed and I have run
into a problem that I haven't been able to figure out. I have written
two classes and I want them to have pointers to each other. So class
One has a private class variable of type Two and class Two has a
private class variable of type One. I #include the header file of the
other class in each file. When I try to build the project it gives me
three errors, all in regards to the line that the variable is declared
(ex: 'One' : missing storage-class or type specifiers). If I change
the order that the classes are included in Main.cpp from:
#include "One.h"
#include "Two.h"

to:
#include "Two.h"
#include "One.h"

then I get errors on the opposite classes (ex: 'Two' : missing
storage-class or type specifiers).
Is this something that I can't do in C++? Is there a problem with
Visual C++? Do I need to change my code or a setting somewhere?
Thanks in advance,
Holden.
 
G

Gianni Mariani

Holden said:
Hello,
I am using Visual C++ 6.0 with Service Pack 5 installed and I have run
into a problem that I haven't been able to figure out. I have written
two classes and I want them to have pointers to each other. So class
One has a private class variable of type Two and class Two has a
private class variable of type One. I #include the header file of the
other class in each file. When I try to build the project it gives me
three errors, all in regards to the line that the variable is declared
(ex: 'One' : missing storage-class or type specifiers). If I change
the order that the classes are included in Main.cpp from:
#include "One.h"
#include "Two.h"

to:
#include "Two.h"
#include "One.h"

then I get errors on the opposite classes (ex: 'Two' : missing
storage-class or type specifiers).
Is this something that I can't do in C++? Is there a problem with
Visual C++? Do I need to change my code or a setting somewhere?
Thanks in advance,
Holden.

You can forward declare the other class ...

// file one.h
class B; // incomplete class decl.

class A
{
public:
B * b;

};


// file two.h
class A; // incomplete class decl

class B
{
public:
A * a;
};


It won't matter which order they are included.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top