Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7818619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T06:38:12+00:00 2026-06-02T06:38:12+00:00

I have a namespace defined in one header file and used in another, but

  • 0

I have a namespace defined in one header file and used in another, but it cannot be found. Specifically, a namespace called “players” defined in “players/Players.hpp” and used in a file called “players/Ownable.hpp” cannot be found in a file called “combat/Targetable.hpp”

The errors are

...\source\combat\Targetable.hpp(7): 'players' : is not a class or namespace name
...\source\combat\Targetable.hpp(7): 'Ownable' : base class undefined

Obviously it’s some syntax thing I don’t understand. I’ve spent some time simplifying the code so it looks silly, but bear with me.

// source/players/Players.hpp:
#ifndef PLAYERS_HPP
#define PLAYERS_HPP

#include "../Headers.hpp"

namespace players {
  class Player{

// this class compiles fine.
// There used to be a "Players.cpp" but it's been simplified away

    public:
      int getID(){ return 0; }
      int getTeam(){ return 0; }
      string getName(){ return ""; }
      Vec3 getColor(){ return Vec3(0.0,0.0,0.0); }
  };
}
#endif

And players/Ownable.hpp, which is in the same folder as Player.hpp and also compiles fine:

// source/players/Ownable.hpp:
#ifndef OWNABLE_HPP
#define OWNABLE_HPP

#include "Players.hpp"

namespace players {
  class Ownable;
  typedef boost::shared_ptr<Ownable> OwnablePTR;
  typedef boost::weak_ptr<Ownable> OwnableWPTR;

  class Ownable {
    public:
      Ownable(){}
      Ownable(int playerID) : playerID(playerID){}
      bool isAlliedWith(OwnablePTR other){ return false; }

    private:
      int playerID;
  };
}

#endif

Here’s where the fun starts. I have a file at “source/combat/Targetable.hpp”, which is in a different directory than the other two. However, the file itself seems to include fine:

// source/combat/Targetable.hpp:
#ifndef TARGETABLE_HPP
#define TARGETABLE_HPP

#include "../players/Ownable.hpp"

namespace combat{
  class Targetable : public players::Ownable { // ERROR
    public:
      Targetable(int playerID){}
      //Targetable(players::Player player);

      virtual Vec2 getPosition(){
        return Vec2();
      }
      virtual Vec2 getVelocity(){
        return Vec2();
      }
  };
}

#endif

I’m really hoping this is some silly syntax thing that I’m missing. I’ve even tried

using players::Ownable;

but that A) pollutes the files that include this one, and B) doesn’t fix anything. Any help?

EDIT: GManNickG got it, it was a circular include in the Headers.hpp file. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T06:38:15+00:00Added an answer on June 2, 2026 at 6:38 am

    You have a circular include.

    First consider the purpose of include guards:

    // a.hpp
    #ifndef A_HPP
    #define A_HPP
    
    // stuff
    
    #endif
    

     

    // b.hpp
    #ifndef B_HPP
    #define B_HPP
    
    #include "a.hpp"
    
    // stuff
    
    #endif
    

     

    // c.hpp
    #ifndef C_HPP
    #define C_HPP
    
    #include "a.hpp"
    #include "b.hpp"
    
    // stuff
    
    #endif
    

     

    // x.cpp
    #include "c.hpp"
    

    The inclusion of c.hpp will end up include a.hpp twice. The first time, the guards are not defined and everything is okay, and the second time the guards prevent redefinitions. This is what we want.

    This does not work when you have a loop, though. (It will prevent it, which is good, but it does “too well” because the guard is defined right after its tested, which means the contents of the header haven’t actually yet been processed). Consider this instead:

    // a.hpp
    #ifndef A_HPP
    #define A_HPP
    
    #include "c.hpp"
    
    // stuff
    
    #endif
    

     

    // b.hpp
    #ifndef B_HPP
    #define B_HPP
    
    #include "a.hpp"
    
    // stuff
    
    #endif
    

     

    // c.hpp
    #ifndef C_HPP
    #define C_HPP
    
    #include "b.hpp"
    
    // stuff
    
    #endif
    

     

    // x.cpp
    #include "c.hpp"
    

    Which is similar to what you have. x.cpp includes c.hpp, which is the first time it’s been included so it defines C_HPP. Then c.hpp includes b.hpp, which includes a.hpp. Then a.hpp includes c.hpp and finds that C_HPP has already been defined, so the include does nothing.

    Assuming a.hpp still manages to compile (that is, c.hpp isn’t actually needed), then a.hpp finishes, then b.hpp finishes, then c.hpp finally actually defines its contents before returning to x.cpp.

    The solution is to minimize the amount of headers you include. Use forward declarations, and most of all: do not use ‘include everything’ headers! These are terrible. And I suspect that’s what Headers.hpp is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have only one namespace in my XML file, defined on the root element.
I have two source files, one named main.cpp (where the namespace M is defined)
I have 4 files, 2 headers and 2 cpp files. Header file one: #ifndef
I have a struct defined in a header file with three other files that
I have a using statement defined like this in codebehind: using Name = Extremely.Long.And.Unwieldy.Namespace
I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
Say I have a template function in namespace A. I also have another namespace
I have a function like this defined in one class: using System; using System.Collections.Generic;
Is it possible to use an alias defined in one file throughout an assembly?
I have two threads One and Two. defined by their respective classes in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.