unordered_map - 未定义模板的隐式实例化

时间:2013-04-14 22:20:44

标签: c++ unordered-map

我的C ++生疏了。我有一个unordered_map<some_enum_type, string>的成员变量。

我正在尝试在类构造函数中填充地图。我在这里做错了什么?

从我的标题:

#include <iostream>
#include <unordered_map>

using namespace std;

typedef enum{
    GET,
    POST,
    PUT,
    DELETE
}http_verb;

class CouchServer{
    string host;
    int port;
    string dbname;
    unordered_map<http_verb,string> req_types;
public:

我的构造函数实现:

 CouchServer::CouchServer(string host, int port, string dbname){
    this->host = host;
    this->port = port;
    this->dbname = dbname;
    this->req_types = {
        {req_types[GET], "GET"},
        {req_types[POST], "POST"},
        {req_types[PUT], "PUT"},
        {req_types[DELETE],"DELETE" }
    };
   }

更新

在阅读提供的答案和评论后,我将标题更改为:

    class CouchServer{
    string host;
    int port;
    string dbname;
    unordered_map<http_verb,string> req_types;
public:
    CouchServer(std::string host, int port, std::string dbname)
    : host(std::move(host))
    , port(port)
    , dbname(std::move(dbname))
    , req_types{
        { http_verb::GET, "GET" },
        { http_verb::POST, "POST" },
        { http_verb::PUT, "PUT" },
        { http_verb::DELETE, "DELETE" }
    }
    {  }

同样的问题仍然存在。我应该提一下,我正在尝试使用XCode 4编译此代码,也就是说 Apple LLVM编译器4.2

1 个答案:

答案 0 :(得分:2)

这可能是编译器限制。在GCC 4.7.2中,类似的东西对我有用,而且标准确实说有一个带有初始化列表的赋值运算符。

但你不应该在构造函数中做任何作业!使用构造函数初始化列表更好:

CouchServer(std::string host, int port, std::string dbname)
: host(std::move(host))
, port(port)
, dbname(std::move(dbname))
, req_types { { http_verb::GET, "GET" } }  // etc.
{  }

(当然,永远不会在头文件中说abusing namespace std;。)

你必须专注std::hash为你的枚举;铸造到合适的整体类型应该可以解决问题。