problems with forward declaration

This is a discussion on problems with forward declaration within the C++ Programming forums, part of the General Programming Boards category; Thanks in advance for looking over this. I'm having serious problems with vc++6. I have 3 vector classes, CVector2, CVector3, ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    6

    problems with forward declaration

    Thanks in advance for looking over this.

    I'm having serious problems with vc++6.

    I have 3 vector classes, CVector2, CVector3, and CVector4. each of them reference both others. before any of them are declared I use

    Code:
    class CVector2; 
    class CVector3; 
    class CVector4;
    then I define the functions in CVector2, CVector3, and CVector4 in that order.
    Everytime I make reference CVector3 or CVector4 in the code for CVector2 it gives me the following (or similar) error:

    error C2027: use of undefined type 'CVector3'
    vector.h(33) : see declaration of 'CVector3'

    How could the type be undefined if the compiler knows it has been declared? I'm utterly confused...

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    why don't you just have main reference them and have any cross-referencing done through main?
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    Originally posted by major_small
    why don't you just have main reference them and have any cross-referencing done through main?

    I'm not sure I entirely understand what you are saying... could you explain this further?

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    First off, such interdependencies are not a good idea. It makes the system more coupled, and harder to change. I am guessing you are referencing them for vector operations, or something of that nature. If that is the case, then consider removing those functions from the classes, and putting them in a separate namespace.

    Now, to the question. From the sound of it, you have something like this:
    Code:
    CVector2;
    
    class CVector3 {
    public:
      void foo(CVector2);
    };
    The problem is, to have an instance of the class, you have to have the class defined, not simply declared. For one, the compiler needs to know how much space to reserve. If you had a pointer or a reference, however, this method would work.
    Code:
    CVector2;
    
    class CVector3 {
    public:
      void foo(CVector2&);
      void foo(CVector2*);
    };
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    I'm beginning to agree that this interdependancy is a bad idea and I will definitely change it.
    However, I am curious as to the reasons I'm getting these errors. I am using references.

    Code:
    class CVector2;
    
    class CVector3 
    {
       
        CVector3(const CVector2& vec) : x(vec.x), y(vec.y), z(0.0)
        { }
    ...

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Originally posted by logicalhippo
    I'm beginning to agree that this interdependancy is a bad idea and I will definitely change it.
    However, I am curious as to the reasons I'm getting these errors. I am using references.

    Code:
    class CVector2;
    
    class CVector3 
    {
       
        CVector3(const CVector2& vec) : x(vec.x), y(vec.y), z(0.0)
        { }
    ...
    Because you are trying to access members of an undefined class. The compiler doesn't know that CVector2 has members called x and y.

    You can separate them into header and source files though.

    header:
    Code:
    class CVector2;
    
    class CVector3
    {
      CVector3(const CVector2&);
    ...
    source:
    Code:
    #include "CVector3.h"
    #include "CVector2.h"
    
    CVector3::CVector3(const CVector2& vec)
    : x(vec.x), y(vec.y), z(0.0 {}
    ...

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    6
    ah yes. that would do it. Thank you.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by Zach L.

    Now, to the question. From the sound of it, you have something like this:
    Code:
    class CVector2;
    
    class CVector3 {
    public:
      void foo(CVector2);
    };
    Actually, that is legal. Incomplete types (types which are declared but not defined) can be used as references, pointers, parameters, or return types. As long as you're only declaring the method, not defining it, it's OK. In a class definition in which you declare but don't define any methods, you can use an incomplete type for anything except:

    1) A base class
    2) A member variable

    Also, you cannot (obviously) use new or delete to create/destroy incomplete types -- this has implications sometimes for how you code your destructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Forward declaration with g++
    By blakaxe in forum Linux Programming
    Replies: 0
    Last Post: 06-15-2009, 09:22 AM
  2. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  3. please help me
    By insane in forum Game Programming
    Replies: 8
    Last Post: 05-12-2003, 12:40 AM
  4. Forward Declarations in .net
    By Cornpops in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2003, 01:22 PM
  5. Forward Declaration
    By BigDaddyDrew in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2003, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21