2011-04-14 18 views
1

對不起,怪題變量名,不太知道什麼叫吧,反正這裏是我的問題:的#include(荷蘭國際集團)的文件,用現在的錯誤類型C++

我有一個錯誤信息:

pserver.h:27: error: ISO C++ forbids declaration of ' myHashMap ' with no type
pserver.h:27: error: expected ' ; ' before ' < ' token

引用這條線在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<string, int>識別爲類型呢?

+0

更多代碼!什麼是hashmap.hpp? – 2011-04-15 00:05:03

+0

@Bob Fincheimer hashmap.hpp定義了myHashMap類 – dubyaa 2011-04-15 00:05:56

+0

這沒有任何意義。你顯示'myHashMap'被定義在某個文件中,然後'#include'hashmap.hpp,那麼如何在hashmap.hpp中再次定義'myHashMap'? – ildjarn 2011-04-15 00:11:10

回答

0

您:

  • 忘記#include <string>
  • 需要使用std::string代替string
  • 需要一個using聲明std::string
  • 需要一個using指令namespace std
  • 沒有正確的包含標題包含你的myHashMap template into pserver.h

或者上面的一些組合。

+0

我有'#include #include using namespace std; #include「pserver.h」 '在我的.cpp文件中 – dubyaa 2011-04-14 23:58:56

+0

最好在頭文件中定義類型,而不是通過「using」使用名稱空間。這會污染其他文件的名稱空間,包括標題。 – 2011-04-15 00:00:53

+0

@dubyaa:pserver.h使用'std :: string',所以它應該'#include '而不依賴其他源文件來獲得正確的包含。同樣,pserver。h使用'std :: string',所以它應該完全限定類名而不依賴其他源文件來使用指令/聲明。你的代碼很瑣碎,這就是它失敗的原因。 – ildjarn 2011-04-15 00:01:01

0

編譯器顯然不知道myHashMap是什麼。您或者忘記將myHashMap的聲明包含到pserver.h(即使您聲稱已包含它),或者您的頭文件正遭受循環包含問題的困擾。另外,是否可以將myHashMap聲明爲某個命名空間的成員?

看起來這個問題與std::string沒有任何關係,與其他海報的建議相反。儘管std::string問題可能存在,但引用的錯誤消息是由編譯器未看到myHashMap聲明引起的。

相關問題