Visual C++ and include order.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Holden

    Visual C++ and include order.

    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.
  • Gianni Mariani

    #2
    Re: Visual C++ and include order.

    Holden wrote:[color=blue]
    > 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.[/color]

    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.

    Comment

    Working...