3
#include <iostream>
#include <map>
#include <string>
using namespace std;
template <class T>
class Counter
{
public:
Counter()
{
totalCount
}
~Counter()
{
}
bool containsKey(T key)
{
map<T, double>::iterator it = counter.find(T);
if (it == counter.end()) return false;
return true;
}
private:
map<T, double> counter;
double totalCount;
};
int main()
{
Counter<string> table;
return 0;
}
此代碼甚至沒有編譯,我無法弄清楚是什麼錯誤。任何幫助,將不勝感激。謝謝!從屬範圍錯誤與stl
cmd以編譯
g++ counter.cpp
的錯誤是
error: need ‘typename’ before ‘std::map<T, double>::iterator’ because ‘std::map<T, double>’ is a dependent scope
我認爲錯誤是給你一個很好的提示 –
'如果(...)返回false;否則返回true;'是反模式。寫'return not ...;'代替。 –
感謝您指出該慣例。將利用它... – shashydhar