Bambino
Bambino

Reputation: 415

Copy assign json-Container to vector

I am trying to convert a json-Container from the JSON-library JSON for Modern C++ to a vector, but it does not work with the =-operator (I get a compiler error "more than one operator "=" matches these operands").

A minimum working example:

#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;
}

You can find the json.hpp here.

Using the constructor with vector<double> copy = j; works and I could write copyWithAssign = copy; but that seems dumb. There must be a direct way to assign j to a vector which has been declared and constructed before.

I thought casting might help since the compiler can't decide which type to use. I tried (vector<double>)j, but that didn't help.

Upvotes: 0

Views: 348

Answers (1)

Bambino
Bambino

Reputation: 415

One should use

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

Credits go to theodelrieu who posted this answer here.

Upvotes: 1

Related Questions