CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2010
    Posts
    47

    Question about includes and namespaces

    In the program I am looking at a library has been included in a .cpp file. In the corresponding header file objects from the library's namespace are being declared but there's no include directive. So how is it possible to use things from the namespace if the include directive is in the .cpp file and not in the header file? If I try to do the same I get unresolved symbol errors in the .h file.

    This is the .cpp file I am looking at:
    http://pastebin.com/5UndEpHi
    And the header.
    http://pastebin.com/CTc8CzpR

    The .cpp file includes "Ogre.h" and the .h file is using the Ogre namespace without including it. How is this possible?


    Thanks a lot for your guidance

  2. #2
    Join Date
    Mar 2010
    Posts
    47

    Re: Question about includes and namespaces

    Additionally, if I move the include directive from the .cpp file to the header I gives me a file not found error about the .h file I want to include. What could cause this?

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Question about includes and namespaces

    It must be including it somewhere.
    Maybe it's setup in the project settings. In Visual Studio, go to the project settings and see if there is an additional include file specified for the compiler.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    Join Date
    Mar 2010
    Posts
    47

    Re: Question about includes and namespaces

    I don't believe so.

    So if the ogre include directive is in a .h file somewhere down the chain of includes you can use the namespace? What I don't understand is why the includes are in the .cpp files and why I get an error if I try to put the include directive in the .h file instead.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question about includes and namespaces

    You only need a forward-declaration rather than a complete class definition (as you'd get with an include) if you are only using pointers or references to a particular type in a header.

    This is one means commonly used to break circular dependencies between header files. If such a circular dependency exists, then putting the include in the header could certainly cause an error.

  6. #6
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Question about includes and namespaces

    Quote Originally Posted by superkemo View Post
    So if the ogre include directive is in a .h file somewhere down the chain of includes you can use the namespace?
    Yes, that's what I meant with "It must be including it somewhere.".
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  7. #7
    Join Date
    Mar 2010
    Posts
    47

    Re: Question about includes and namespaces

    I'm still stuck with this, I can't work out what I'm missing. I'm going to post the beginning of each of the files I'm working with in the input system. I have access to the OIS namespace in every file except the one I need it in (object.h). I really hope someone can see where I'm going wrong. I've been trying to follow the layout of the other systems in the framework (eg. copying how things were set up in the graphics system).

    Object.ccp

    Code:
    #include "OIS.h"
    
    //
    // core includes
    //
    #include "..\BaseTypes\BaseTypes.h"
    #include "..\Interfaces\Interface.h"
    
    //
    // Input system includes
    //
    #include "Scene.h"
    #include "Object.h"
    
    InputObject::InputObject(ISystemScene* pSystemScene) : ISystemObject( pSystemScene, NULL )
    {
    }
    
    InputObject::InputObject(ISystemScene* pSystemScene, pcstr pszType, pcstr pszName) : ISystemObject( pSystemScene, NULL )
    {
    }
    
    etc...
    Object.h

    Code:
    #pragma once
    
    class InputSystem;
    class InputScene;
    class InputTask;
    
    ///////////////////////////////////////////////////////////////////////////////
    /// <summary>
    ///   Implementation of the ISystemObject interface.
    ///   See Interfaces\System.h for a definition of the class and its functions.
    /// </summary>
    ///////////////////////////////////////////////////////////////////////////////
    
    class InputObject : public ISystemObject, public OIS::MouseListener, public OIS::KeyListener, public OIS::JoyStickListener
    {
    	friend class InputSystem;
        friend class InputScene;
    
    public:
    	
    	virtual void Update( f32 DeltaTime );
    
    etc...
    Scene.cpp

    Code:
    #include "OIS.h"
    //
    // core includes
    //
    #include "..\BaseTypes\BaseTypes.h"
    #include "..\Interfaces\Interface.h"
    
    //
    // Input system includes
    //
    #include "System.h"
    #include "Scene.h"
    #include "Task.h"
    #include "Object.h"
    
    #include <tchar.h>
    
    #include "D3DX9Math.h"
    
    InputScene::InputScene(
        ISystem* pSystem
        )
        : ISystemScene( pSystem )
        , m_pInputTask( NULL )
    {
       
        
    }
    
    etc...
    Scene.h

    Code:
    #pragma once
    
    #define DIRECTINPUT_VERSION 0x0800
    #include "dinput.h"
    #include "System.h"
    
    class InputSystem;
    class InputTask;
    class InputObject;
    
    ///////////////////////////////////////////////////////////////////////////////
    /// <summary>
    ///   <c>InputScene</c> Implementation of the ISystemScene interface.
    ///   The input scene contains all the input objects
    /// </summary>
    ///////////////////////////////////////////////////////////////////////////////
    
    class InputScene : public ISystemScene
    {
        friend class InputSystem;
        friend class InputTask;
    
    public:
    
    protected:
    
        InputScene( ISystem* pSystem );
        ~InputScene( void );
    
    etc...
    System.ccp

    Code:
    #include "OIS.h"
    
    //
    // core includes
    //
    #include "..\BaseTypes\BaseTypes.h"
    #include "..\Interfaces\Interface.h"
    
    //
    // Input system includes
    //
    #include "System.h"
    #include "Scene.h"
    
    InputSystem::InputSystem(
        void
        )
        : ISystem()
    {
    }
    
    etc...
    System.h

    Code:
    #pragma once
    
    
    ///////////////////////////////////////////////////////////////////////////////
    /// <summary>
    ///   <c>InputSystem</c> Implementation of the ISystem interface for generic 
    ///   Input functionality.
    /// </summary>
    ///////////////////////////////////////////////////////////////////////////////
    
    class InputScene;
    class InputTask;
    
    class InputSystem : public ISystem
    {
    	friend InputScene;
    
    protected:
    
    etc...
    SystemInput.ccp

    Code:
    
    #include <windows.h>
    //
    // extern includes
    //
    #pragma warning( push, 0 )
    // Temporarily switching warning level to 0 to ignore warnings in extern/Ogre
    #include "OIS.h"
    #pragma warning( pop )
    
    //
    // core includes
    //
    #include "..\BaseTypes\BaseTypes.h"
    #include "..\Interfaces\Interface.h"
    
    //
    // system includes
    //
    #include "System.h"
    
    
    ManagerInterfaces   g_Managers;
    
    
    BOOL APIENTRY
    DllMain(
        HMODULE hModule,
        DWORD Reason,
        LPVOID pReserved
        )
    {
        UNREFERENCED_PARAM( hModule );
        UNREFERENCED_PARAM( pReserved );
    
        switch ( Reason )
        {
        case DLL_PROCESS_ATTACH:
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
        }
    
        return TRUE;
    }
    
    
    extern "C" void __stdcall
    InitializeInput(
        ManagerInterfaces* pManagers
        )
    {
        g_Managers = *pManagers;
    }
    
    
    extern "C" ISystem* __stdcall
    CreateInputSystem(
        Debug::Debugger* p_Debugger
        )
    {
    	Debug::Init( p_Debugger );
        return new InputSystem();
    }
    
    
    extern "C" void __stdcall
    DestroyInputSystem(
        ISystem* pSystem
        )
    {
    	delete reinterpret_cast<InputSystem*>( pSystem );
    }
    I'm sorry to post so much here, I'm hoping it will just take a quick glance at the includes for someone who knows what they are doing to see the problem.

    Note that currently
    Code:
    class InputObject : public ISystemObject, public OIS::MouseListener, public OIS::KeyListener, public OIS::JoyStickListener
    is giving me a series of errors because OIS is an undefined symbol.

    A huge thank you to anyone who can help me and thanks also to everyone that has replied so far

  8. #8
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Question about includes and namespaces

    Can't you just include "OIS.h" in the header where you need the namespace?
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured