声明将字符串映射到模板的unordered_map时出错

时间:2022-04-25 09:43:11
template<typename T>
struct hash;

template<typename T> class WUG{
    public:
        WUG(){
            unordered_map<string,typename T,hash> vertexmap; //Problem
        }
};

I am getting some error in this code which really doesn't say anything to me. How should I declare it properly?

我在这段代码中遇到了一些错误,这对我来说真的没有任何意义。我该如何正确申报?

The error message I'm having is:
[Error] template argument 2 is invalid
[Error] template argument 5 is invalid

我遇到的错误信息是:[错误]模板参数2无效[错误]模板参数5无效

1 个解决方案

#1


1  

You have using namespace std; that imports all the names from the std namesapce including std::hash, fix:

你有使用命名空间std;从std namesapce导入所有名称,包括std :: hash,fix:

struct MyHash;

template <typename T>
class WUG {
public:
  WUG() {
    unordered_map<string, T, MyHash> vertexmap; //Problem
  }
};

or with the standard hash:

或者使用标准哈希:

template <typename T>
class WUG {
public:
  WUG() {
    unordered_map<string, T> vertexmap; //Problem
  }
};

#1


1  

You have using namespace std; that imports all the names from the std namesapce including std::hash, fix:

你有使用命名空间std;从std namesapce导入所有名称,包括std :: hash,fix:

struct MyHash;

template <typename T>
class WUG {
public:
  WUG() {
    unordered_map<string, T, MyHash> vertexmap; //Problem
  }
};

or with the standard hash:

或者使用标准哈希:

template <typename T>
class WUG {
public:
  WUG() {
    unordered_map<string, T> vertexmap; //Problem
  }
};