C++ 编译器未找到命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10112445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Namespaces not found by the compiler
提问by Josh C
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
cpp文件
chin.cpp
cpp文件
chout.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.cpptoo.
我命名空间是戎,charon_in和charon_out。主要问题出现在特定文件charon.cpp 上,因此该文件和chin.cpp也是如此。
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
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
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.)
(我知道将类放在 cpp 文件中并不是一件标准的事情,但我知道这样做是可能的,所以这就是我选择尝试的原因。)
I can't see what mistakes I've made. Any help would be appreciated.
我看不出我犯了什么错误。任何帮助,将不胜感激。
回答by David Rodríguez - dribeas
You need to include the header file that declares the namespace (or declare it again) before the using directive
:
您需要在以下内容之前包含声明命名空间(或再次声明)的头文件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.
正如 Wayne 正确指出的那样,您可能希望以不同的方式重构您的代码,包括将包含声明并可包含在不同翻译单元中的头文件。
回答by Wayne Tanner
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.
您在 .cpp 文件中有类声明和定义。您需要将类声明移动到 .h 文件并将其包含在使用该类的相应文件中。
For example, move the following to chin.h and include chin.h in charon.cpp.
例如,将以下内容移动到 chin.h 并在 charon.cpp 中包含 chin.h。
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();
};
}