Thread: forward declaration

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    forward declaration

    Hello

    I'm having problems with compiling..
    The error I'm getting is: error C2079: 'A::m_b' uses undefined class 'B'


    header1:

    Code:
    #include "header2.hpp"
    class B;
    class A {
    public:
    	B m_b;
    };
    header2:

    Code:
    #include "header1.hpp"
    class A;
    class B {
    public:
    	A &m_a;
    };
    What am I doing wrong?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think that it should be:
    Code:
    #include "header2.hpp"
    
    class A {
    public:
    	B m_b;
    };
    Code:
    class A;
    class B {
    public:
    	A &m_a;
    };
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    A has a value member of type B. Therefore, it needs the entire definition. The declaration is only enough for the reference member, as B has.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    630
    Now I get 3 errors:
    error C2146: syntax error : missing ';' before identifier 'm_b'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by CornedBee View Post
    A has a value member of type B. Therefore, it needs the entire definition. The declaration is only enough for the reference member, as B has.
    What should I do then?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Now I get 3 errors:
    What code did you try? Note that you did not include header guards in your original example, so I omitted them as well.

    What should I do then?
    CornedBee basically explained what I illustrated.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks for help..
    I found out there was another class (C) that was causing the problem when I changed it to what you said.

    I had to change class C in the same way, and now it works.
    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM