2017-06-14 114 views
0

我试图从JSON库JSON for Modern C++一个json - 集装箱转换为vector,但它不与= - 运算符的工作(我得到一个编译错误“不止一个运营商“=”匹配这些操作数“)。复制分配JSON-容器矢量

最小工作示例:

#include "json.hpp" 

using json = nlohmann::json; 
using namespace std; 

int main() 
{ 
    vector<double> v = { 0 , 10 , 20 , 100 }; 
    json j(v); 

    vector<double> copy = j; 

    vector<double> copyWithAssign; 
    //copyWithAssign = j; // more than one operator "=" matches these operands 

    return 0; 
} 

您可以找到json.hpp here

使用构造函数与vector<double> copy = j;作品,我可以写copyWithAssign = copy;但这似乎愚蠢。必须有一种直接的方式将j分配给之前已经声明和构建的vector

我以为铸造可能会有所帮助,因为编译器无法决定使用哪种类型。我试过(vector<double>)j,但那没有帮助。

+0

请不要链接到下载链接。恐慌随之而来。 – NathanOliver

+0

我可以轻松删除链接,但我希望读者能够尽可能轻松地运行我的最低工作示例。我在删除链接后如何执行此操作? (顺便说一下,为什么将下载链接发布到头文件不好?) – Bambino

+0

仅仅因为它说它是头文件并不意味着它是。你可以链接到文本,然后如果人们想要它,他们可以选择下载它或将其复制到他们自己的文件中。例如[此链接](https://github.com/nlohmann/json/blob/develop/src/json.hpp)链接到源代码而不下载 – NathanOliver

回答

1

应该使用

copyWithAssign = j.get<vector<double>>(); 

现金去theodelrieu谁张贴了这个答案here