【问题标题】:#include(ing) files, variable name with now type error C++#include(ing) 文件,带有现在类型错误 C++ 的变量名
【发布时间】:2011-04-14 23:51:17
【问题描述】:

对不起,奇怪的标题,不太清楚该叫什么,无论如何,这是我的问题:

我收到一条错误消息:

pserver.h:27: 错误:ISO C++ 禁止声明没有类型的“myHashMap
pserver.h:27: 错误:在“<”令牌之前需要“;

在 pserver.h 中引用这一行:

template <typename K, typename V>
class myPserver {
public:
    //
private:
    myHashMap<string, int> theMap; // line 27
};

myHashMap&lt;K, V&gt; 类在单独的文件中定义为

template <typename K, typename V>
class myHashMap {
    //
};
#include "hashmap.hpp"

该类的头文件包含在 pserver.h 中。

那么为什么编译器不能将myHashMap&lt;string, int&gt; 识别为一种类型呢?

【问题讨论】:

  • 更多代码! hashmap.hpp 中有什么?
  • @Bob Finchemer hashmap.hpp 定义了 myHashMap 类
  • 这毫无意义。您显示myHashMap 在某个文件中定义,然后#includes hashmap.hpp,那么myHashMap 如何在 hashmap.hpp 中再次定义?
  • @ildjarn 他的模板方法是在hpp 中定义的,而不是在类中。

标签: c++ class object


【解决方案1】:

编译器显然不知道myHashMap 是什么。您要么忘记将myHashMap 的声明包含在pserver.h 中(即使您声称您确实包含了它),或者您的头文件存在循环包含问题。另外,myHashMap 是否被声明为某个命名空间的成员?

看起来这个问题与std::string 没有任何关系,这与其他海报的建议相反。尽管std::string 的问题可能存在,但您引用的错误消息是由编译器没有看到myHashMap 的声明引起的。

【讨论】:

    【解决方案2】:

    你要么:

    • 忘了#include &lt;string&gt;
    • 需要使用std::string而不是string
    • 需要std::string 的 using 声明
    • 需要 namespace std 的 using 指令
    • 没有正确将包含myHashMap 模板的标头包含到 pserver.h 中

    或以上的某种组合。

    【讨论】:

    • 我的 .cpp 文件中有 #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; #include "pserver.h"
    • 最好将你的类型限定在头文件上,而不是通过“using”来使用命名空间。这会污染包括头文件在内的其他文件的命名空间。
    • @dubyaa : pserver.h 使用std::string,所以它应该#include &lt;string&gt; 而不依赖其他源文件来获得正确的包含。同样,pserver.h 使用std::string,因此它应该完全限定类名,而不依赖其他源文件来拥有 using 指令/声明。你的代码很草率,这就是它失败的原因。
    • @dubyaa - 它们必须包含在您声明“theMap”的头文件中。
    • 我尝试将#include &lt;string&gt; 添加到头文件中,并使用std::string 以及它看起来的所有方式
    猜你喜欢
    • 1970-01-01
    • 2011-06-23
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-03-05
    相关资源
    最近更新 更多