Thread: is not a class or namespace name (But i'm sure it is)

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    is not a class or namespace name (But i'm sure it is)

    Hello everyone,

    I'm new here but i already got some c++ and oop experience.

    My problem is that i got a lot of errors when i am compiling but i don't see where i'm wrong.

    It looks like the compiler can't find the required classes but i included the headers.

    Can someone see what i do wrong?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    GameState.h includes GameStateManager.h and vice versa. You'll need to break the circular dependency by not including the other in one header.

    It appears that the declaration of CGameState doesn't need to know anything about CGameStateManager. You'll need to move the implementation of at least those methods that use CGameStateManager to a separate source file.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Quote Originally Posted by anon View Post
    GameState.h includes GameStateManager.h and vice versa. You'll need to break the circular dependency by not including the other in one header.

    It appears that the declaration of CGameState doesn't need to know anything about CGameStateManager. You'll need to move the implementation of at least those methods that use CGameStateManager to a separate source file.
    I used
    Code:
    #pragma once
    so i thought it would be fine.

    Seems like include in c++ is not the same as in PHP.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    #include means: take this file and paste it at the place of the macro (because of the header guards - if it hasn't been included in this compilartion unit already).

    So, OgreTest.cpp includes GameStateManager.h. GameStateManager.h includes GameState.h. GameState.h has #include "GameStateManager.h", but that header has already been included for OgreTest.cpp.

    So, for OgreTest.cpp the compilation unit that the compiler will work with contains:

    Code:
    class CGameState
    {
        //...
        //using CGameStateManager
    };
    
    class CGameStateManager
    {
         //...
    };
    
    int main()
    {
        //...
    }
    As you can see, CGameState ends up using CGameStateManager which will be declared later on. In C++ you can use only things that have been declared / defined earlier.

    The solution is not to rearrange includes carefully. The solution is to separate declarations and implementations, and use forward declarations in more complicated cases (not the case here).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM