命名空间 'bar' 中的 'Foo' 没有为头文件中的对象成员命名类型
'Foo' in namespace 'bar' does not name a type for object member in header file
作为序言,我使用 eclipse c++ 作为 IDE。我正在使用 c++0x11 标准。 (所以我可以使用互斥量)我是 C++ 的新手,但之前做过一些 C 并且非常熟悉 Java 编程。另外,我知道 .h 通常不是 C++ 文件的类型。
我试图在我的 class stellad::Dispatcher 中包含 stellad::KeyHook 的私有对象成员,但在构建时出现以下错误:
Building file: ../src/KeyHook.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/KeyHook.d" -MT"src/KeyHook.d" -o "src/KeyHook.o" "../src/KeyHook.cpp"
In file included from ../src/KeyHook.h:10:0,
from ../src/KeyHook.cpp:8:
../src/Dispatcher.h:23:11: error: ‘KeyHook’ in namespace ‘stellad’ does not name a type
stellad::KeyHook keyhook;
^
src/subdir.mk:21: recipe for target 'src/KeyHook.o' failed
make: *** [src/KeyHook.o] Error 1
许多行已被删除以减少噪音,例如不必要的包含、原型和函数声明。
Dispatcher.h
/*
* Dispatcher.h
*/
#ifndef DISPATCHER_H_
#define DISPATCHER_H_
#include "KeyHook.h"
namespace stellad {
class Dispatcher {
private:
..
stellad::KeyHook keyhook;
public:
Dispatcher();
virtual ~Dispatcher();
..
};
} /* namespace stellad */
int main(int argc, const char* argv[]);
#endif /* DISPATCHER_H_ */
KeyHook.h
/*
* KeyHook.h
*/
#ifndef KEYHOOK_H_
#define KEYHOOK_H_
#include "Dispatcher.h"
namespace stellad {
class KeyHook{
private:
..
public:
KeyHook();
virtual ~KeyHook();
..
};
} /* namespace stellad */
#endif /* KEYHOOK_H_ */
您有一个循环包含问题。从 KeyHook.h 中删除 #include "Dispatcher.h"
。您可能需要添加前向声明 class Dispatcher;
。
这是每个文件都包含另一个文件造成的。
如果第一个包含的是 KeyHook.h
,那么在任何声明之前它包含 Dispatcher.h
。这再次包括 KeyHook.h
但它发现 KEYHOOK_H_
已经定义并且没有声明任何东西。然后 headers 看起来像这样:
// #include "KeyHook.h" from KeyHook.cpp
// #define KEYHOOK_H_
// #include "Dispatcher.h" from KeyHook.h
// #define DISPATCHER_H_
// #include "KeyHook.h" from Dispatcher.h
// KEYHOOK_H_ already declared
// end of #include "KeyHook.h" from Dispatcher.h
namespace stellad {
class Dispatcher {
private:
..
stellad::KeyHook keyhook; // KeyHook not declared here
public:
Dispatcher();
virtual ~Dispatcher();
..
};
} /* namespace stellad */
int main(int argc, const char* argv[]);
// end of #include "Dispatcher.h" from KeyHook.h
namespace stellad {
class KeyHook{
private:
..
public:
KeyHook();
virtual ~KeyHook();
..
};
} /* namespace stellad */
要解决这个问题,需要打破循环包含。 KeyHook
不需要 Dispatcher
,只需从中删除 #include "Dispatcher.h"
。
作为序言,我使用 eclipse c++ 作为 IDE。我正在使用 c++0x11 标准。 (所以我可以使用互斥量)我是 C++ 的新手,但之前做过一些 C 并且非常熟悉 Java 编程。另外,我知道 .h 通常不是 C++ 文件的类型。
我试图在我的 class stellad::Dispatcher 中包含 stellad::KeyHook 的私有对象成员,但在构建时出现以下错误:
Building file: ../src/KeyHook.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/KeyHook.d" -MT"src/KeyHook.d" -o "src/KeyHook.o" "../src/KeyHook.cpp"
In file included from ../src/KeyHook.h:10:0,
from ../src/KeyHook.cpp:8:
../src/Dispatcher.h:23:11: error: ‘KeyHook’ in namespace ‘stellad’ does not name a type
stellad::KeyHook keyhook;
^
src/subdir.mk:21: recipe for target 'src/KeyHook.o' failed
make: *** [src/KeyHook.o] Error 1
许多行已被删除以减少噪音,例如不必要的包含、原型和函数声明。
Dispatcher.h
/*
* Dispatcher.h
*/
#ifndef DISPATCHER_H_
#define DISPATCHER_H_
#include "KeyHook.h"
namespace stellad {
class Dispatcher {
private:
..
stellad::KeyHook keyhook;
public:
Dispatcher();
virtual ~Dispatcher();
..
};
} /* namespace stellad */
int main(int argc, const char* argv[]);
#endif /* DISPATCHER_H_ */
KeyHook.h
/*
* KeyHook.h
*/
#ifndef KEYHOOK_H_
#define KEYHOOK_H_
#include "Dispatcher.h"
namespace stellad {
class KeyHook{
private:
..
public:
KeyHook();
virtual ~KeyHook();
..
};
} /* namespace stellad */
#endif /* KEYHOOK_H_ */
您有一个循环包含问题。从 KeyHook.h 中删除 #include "Dispatcher.h"
。您可能需要添加前向声明 class Dispatcher;
。
这是每个文件都包含另一个文件造成的。
如果第一个包含的是 KeyHook.h
,那么在任何声明之前它包含 Dispatcher.h
。这再次包括 KeyHook.h
但它发现 KEYHOOK_H_
已经定义并且没有声明任何东西。然后 headers 看起来像这样:
// #include "KeyHook.h" from KeyHook.cpp
// #define KEYHOOK_H_
// #include "Dispatcher.h" from KeyHook.h
// #define DISPATCHER_H_
// #include "KeyHook.h" from Dispatcher.h
// KEYHOOK_H_ already declared
// end of #include "KeyHook.h" from Dispatcher.h
namespace stellad {
class Dispatcher {
private:
..
stellad::KeyHook keyhook; // KeyHook not declared here
public:
Dispatcher();
virtual ~Dispatcher();
..
};
} /* namespace stellad */
int main(int argc, const char* argv[]);
// end of #include "Dispatcher.h" from KeyHook.h
namespace stellad {
class KeyHook{
private:
..
public:
KeyHook();
virtual ~KeyHook();
..
};
} /* namespace stellad */
要解决这个问题,需要打破循环包含。 KeyHook
不需要 Dispatcher
,只需从中删除 #include "Dispatcher.h"
。