Namespaces not found by the compiler

38,443

Solution 1

You need to include the header file that declares the namespace (or declare it again) before the using directive:

// using namespace test;  // Error test is not known to be a namespace
namespace test {}
using namespace test;     // Fine -- test is known

As Wayne correctly points out, you probably want to restructure your code differently, including header files that will contain the declarations and can be included in different translation units.

Solution 2

You have the class declarations and the definitons in the .cpp files. You need to move the class declarations to a .h file and include it in the appropriate files that are using the class.

For example, move the following to chin.h and include chin.h in charon.cpp.

namespace charon_in 
{ 

  class chin_ 
  { 
  private: 
    bool pause; 

    iostream key_sequence; 
    deque<char> key_queue; 
    charon_* engine; 

    inline iostream grab(); 


  public: 
    chin_(const charon_& handle); 
    chin_(const chin_& orig); 
    ~chin_(); 

    void refresh(); 
    bool stop_check(); 
  }; 
}

Solution 3

Be wary of cyclic dependencies for they can also cause the compiler to not find the namespace you require.

     //In rightWing.h
     include "leftWing.h"
     // code

       //In leftWing.h
     include "rightWing.h"
     // code
Share:
38,443
Josh C
Author by

Josh C

Updated on March 15, 2021

Comments

  • Josh C
    Josh C about 3 years

    I have several files, listed just below, wherein I define some namespaces and classes. Then in one of the files I am trying to incorporate the classes from the others. The issue is that the compiler seems to not be able to find my namespaces.

    charon.cpp

    chin.cpp

    chout.cpp

    main.cpp

    My namespaces are charon, charon_in, and charon_out. The main problems come up on a particular file, charon.cpp, so here is that file and chin.cpp too.

    The errors:

    g++    -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/sys/charon.o.d -o build/Debug/GNU-Linux-x86/sys/charon.o sys/charon.cpp
    sys/charon.cpp:6:17: error: ‘charon_in’ is not a namespace-name
    sys/charon.cpp:6:26: error: expected namespace-name before ‘;’ token
    sys/charon.cpp:7:17: error: ‘charon_out’ is not a namespace-name
    sys/charon.cpp:7:27: error: expected namespace-name before ‘;’ token
    sys/charon.cpp:15:5: error: ‘chout_’ does not name a type
    sys/charon.cpp:16:5: error: ‘chin_’ does not name a type
    sys/charon.cpp:31:39: error: ‘chin_’ has not been declared
    sys/charon.cpp:31:55: error: ‘engine_input’ was not declared in this scope
    sys/charon.cpp:32:40: error: ‘chout_’ has not been declared
    sys/charon.cpp:32:57: error: ‘engine_output’ was not declared in this scope
    

    charon.cpp

    #include <iostream>
    #include <boost/thread.hpp>
    #include <boost/date_time.hpp>
    
    using namespace std;
    using namespace charon_in;
    using namespace charon_out;
    
    namespace charon
    {
      class charon_
      {
      private:
        chout_ engine_output;
        chin_ engine_input;
        boost::thread input_thread;
        boost::thread output_thread;
        void start_threads();
        void stop_threads();
      public:
        //Methods
    };
    

    chin.cpp

    #include <iostream>
    #include <borland/conio.h>
    #include <ncurses.h>
    #include <boost/thread.hpp>
    
    using namespace std;
    using namespace charon;
    using namespace charon_out;
    
    namespace charon_in
    {
      class chin_
      {
      private:
        bool pause;
        charon_* engine;
        inline iostream grab();
      public:
        //Methods
    };
    

    I triple checked everything. So I am pretty confident the syntax is correct, but obviously I'm missing some key concept otherwise the compiler wouldn't complain.

    (I know putting classes in the cpp files isn't a standard thing to do, but I know it is possible to do it so that's why I chose to try.)

    I can't see what mistakes I've made. Any help would be appreciated.