Visual C++ and include order.

Discussion in 'C++' started by Holden, Sep 15, 2003.

  1. Holden

    Holden Guest

    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.
     
    Holden, Sep 15, 2003
    #1
    1. Advertising

  2. Holden wrote:
    > 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.
     
    Gianni Mariani, Sep 15, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tuckers
    Replies:
    18
    Views:
    626
    ThosRTanner
    May 18, 2005
  2. Johannes Bauer

    Include guards and inclusion order

    Johannes Bauer, Dec 1, 2007, in forum: C++
    Replies:
    18
    Views:
    644
    James Kanze
    Dec 3, 2007
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    946
    Andreas Bogenberger
    Feb 22, 2008
  4. Adrian
    Replies:
    13
    Views:
    646
    Yannick Tremblay
    Mar 31, 2008
  5. Stephan Kämper
    Replies:
    2
    Views:
    240
    Stephan Kämper
    Jan 18, 2004
Loading...

Share This Page