c++ - 用作值的共享指针 - 赋值期间出错

标签 c++ boost

我有一个这样定义的共享指针

typedef share_ptr<boost::unordered_map<int,string>> T;
boost::unordered_map<int,T> webData;

...
webData[100]=T(new boost::unordered_map<int,string>(make_pair(100,"json data returned")));

我总是在这个 webData 处出错线 no instance of boost::unordered_map<K,T,H,P,A>....matches the argument list

最佳答案

boost::unordered_map没有接受 std::pair 实例作为参数的构造函数。要消除错误,必须编写如下内容

#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>
#include <string>

typedef boost::shared_ptr<boost::unordered_map<int, std::string> > T;

int main() {
    boost::unordered_map<int, T> webData;
    webData[100] = T(new boost::unordered_map<int, std::string>());
    webData[100]->insert(std::make_pair(100, "json data returned"));
    return 0;
}

coliru 上的相同代码是here .

关于c++ - 用作值的共享指针 - 赋值期间出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29136861/

相关文章:

c++ - boost::multiprecision::gmp_float::operator =?中的神秘stackoverflow异常?

c++ - 使用 boost::regex 匹配两个完整的单词

visual-studio - 使用 Visual Studio 测试适配器 boost 单元测试 - 设置工作目录

c++ - 如何在 Windows 上修复 VS 2019 中的 SFML 错误?

c++ - 如何使用 GPU 乘以 2 个 OpenCV 垫

c++ - std::string 分配何时在初始化时发生

python - 使用 Boost::Python 进行高阶编程

c++ - 在C++中与libmongoclient链接时出错

c++ - VideoCapture 未检测到 uEye 摄像头

c++ - 从 std :vector 获取数组