相关疑难解决方法(0)

使用[]时,为什么C++ map类型参数需要空构造函数?

另请参见 C++标准列表和默认构造类型

不是一个主要问题,只是烦人,因为我不希望我的类在没有特定参数的情况下被实例化.

#include <map>

struct MyClass
{
    MyClass(int t);
};

int main() {
    std::map<int, MyClass> myMap;
    myMap[14] = MyClass(42);
}
Run Code Online (Sandbox Code Playgroud)

这给了我以下g ++错误:

/usr/include/c++/4.3/bits/stl_map.h:419:错误:没有用于调用'MyClass()'的匹配函数

如果我添加一个默认构造函数,这编译很好; 我确信它不是由不正确的语法引起的.

c++ dictionary

88
推荐指数
3
解决办法
3万
查看次数

标签 统计

c++ ×1

dictionary ×1