c++ - 尝试将对象放入 unordered_map

标签 c++ object stl unordered-map

我正在尝试创建一个 unordered_map,它有一个整数作为键,一个 Transition 对象作为值......这是我所拥有的:

实例化

unordered_map<int, Transition> transitions;

过渡类声明:

class Transition{

    public:
            Transition(int n, char r, char m);
            ~Transition();
            int getNextState();
            char getReplacement();
            char getMovement();

    private:
            int nextState;
            char replacement;
            char movement;
};

向 map 添加过渡

// Create transition object
Transition t(r,b,x);
transitions[keyForMap] = t;

我收到这个错误:

/usr/include/c++/4.7/bits/hashtable_policy.h:445:24: error: no matching function for call to ‘Transition::Transition()’
/usr/include/c++/4.7/bits/hashtable_policy.h:445:24: note: candidates are:
In file included from ball_p1.cpp:6:0:
Transition.h:4:3: note: Transition::Transition(int, char, char)
Transition.h:4:3: note:   candidate expects 3 arguments, 0 provided
Transition.h:1:7: note: constexpr Transition::Transition(const Transition&)
Transition.h:1:7: note:   candidate expects 1 argument, 0 provided

我是否需要以某种方式指定构造函数在实例化中某处采用的参数?我究竟做错了什么?感谢您提前提供帮助。

最佳答案

我总是使用 map.insert(std::make_pair(keyForMap, transition));

关于c++ - 尝试将对象放入 unordered_map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376803/

相关文章:

c++ - 如何优化字符串复制(内存分配)?

c++ - 调试线程时printf的作用

r - 您可以在 R-Studio 中从 Debug模式恢复对象吗?

c++ - 如何使用 "vector of vector"?

c++ - 模板类的模板构造函数的实例化

c++ - 向类 : using macro function into header file 添加动态方法

javascript - 使用嵌入或对象标签中的元素

java - 使用dozer复制java对象

STL - 我可以在 DriverKit 驱动程序中使用 STL 吗?

c++ - 按索引访问 map 值