#include(ing) 文件,变量名称现在类型错误 C++
抱歉,这个奇怪的标题,不太确定该怎么称呼它,无论如何,这是我的问题:
我收到一条错误消息:
pserver.h:27: 错误:ISO C++ 禁止声明无类型的“
myHashMap
”
pserver.h:27:错误:在“<
”标记之前应有“;
”
pserver.h:27: 错误:在 pserver.h 中引用此行的
template <typename K, typename V>
class myPserver {
public:
//
private:
myHashMap<string, int> theMap; // line 27
};
:其中 myHashMap<; K, V>
类在单独的文件中定义为
template <typename K, typename V>
class myHashMap {
//
};
#include "hashmap.hpp"
该类的头文件包含在 pserver.h 中。
那么为什么编译器不能将 myHashMap
识别为类型呢?
Sorry for the strange title, wasn't quite sure what to call it, anyway, here is my question:
I am having an error message:
pserver.h:27: error: ISO C++ forbids declaration of '
myHashMap
' with no type
pserver.h:27: error: expected ';
' before '<
' token
referencing this line in pserver.h:
template <typename K, typename V>
class myPserver {
public:
//
private:
myHashMap<string, int> theMap; // line 27
};
Where the myHashMap<K, V>
class is defined in a separate file as
template <typename K, typename V>
class myHashMap {
//
};
#include "hashmap.hpp"
The header file for this class is included in pserver.h.
So why won't the compiler recognize myHashMap<string, int>
as a type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编译器显然不知道
myHashMap
是什么。您要么忘记将myHashMap
声明包含到pserver.h
中(即使您声称确实包含了它),要么您的头文件存在循环包含问题。另外,myHashMap
是否可能被声明为某个名称空间的成员?与其他发帖者的建议相反,该问题似乎与 std::string 没有任何关系。虽然
std::string
的问题可能存在,但您引用的错误消息是由编译器没有看到myHashMap
的声明引起的。The compiler obviously does not know what
myHashMap
is. You either forgot to include the declaration ofmyHashMap
intopserver.h
(even though you claim that you did include it), or your header files are suffering from circular inclusion problem. Also, could it be thatmyHashMap
is declared as a member of some namespace?It doesn't look like the problem has anything to do with
std::string
, contrary to what other posters suggested. Although the problem withstd::string
might be there, the error messages you quoted are caused by the compiler not seeing the declaration ofmyHashMap
.您可以:
#include
std::string
而不是string
std 的 using 声明::string
namespace std
的 using 指令myHashMap
模板的标头包含到 pserver.h 中或者以上的某种组合。
You either:
#include <string>
std::string
instead ofstring
std::string
namespace std
myHashMap
template into pserver.hOr some combination of the above.