Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: no matching function for call to ‘nlohmann::basic_json<>::value(int, std::set<int>&)’ #2671

Closed
leonardltk opened this issue Mar 11, 2021 · 7 comments
Labels
state: needs more info the author of the issue needs to provide more details

Comments

@leonardltk
Copy link

Hi, i have a hashmap that i want to serialize/deserialize to/from JSON.
here is my code snippet:

#include <iostream>
#include "nlohmann/json.hpp"
#include <unordered_map>
#include <set>
using namespace std;

using json = nlohmann::json;

int main(){

    unordered_map <int, set<int>> shash;
    shash[3] = { 1 };
    shash[3].insert(33);
    shash[4] = { 44 };
    /*
    shash = {
        3 : {1,33},
        4 : {44},
    }
    */

    json jdEmployees = shash;

    set<int> nullSet;
    nullSet.insert(0);
    set<int> ValueSet = jdEmployees.value(3, nullSet);

    return 0;
}

I compiled it like this:

g++ -o ./debug.out -std=c++11 test.cpp nlohmann/json.hpp && ./debug.out

However i get this error:

test.cpp: In function ‘int main()’:
test.cpp:26:53: error: no matching function for call to ‘nlohmann::basic_json<>::value(int, std::set<int>&)’
     set<int> ValueSet = jdEmployees.value(3, nullSet);
                                                         ^
In file included from test.cpp:2:0:
nlohmann/json.hpp:20279:15: note: candidate: template<class ValueType, typename std::enable_if<(nlohmann::detail::is_getable<nlohmann::basic_json<>, ValueType>::value && (! std::is_same<nlohmann::detail::value_t, ValueType>::value)), int>::type <anonymous> > ValueType nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::value(const typename nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::object_t::key_type&, const ValueType&) const [with ValueType = ValueType; typename std::enable_if<(nlohmann::detail::is_getable<nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>, ValueType>::value && (! std::is_same<nlohmann::detail::value_t, ValueType>::value)), int>::type <anonymous> = <enumerator>; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>]
     ValueType value(const typename object_t::key_type& key, const ValueType& default_value) const
               ^
nlohmann/json.hpp:20279:15: note:   template argument deduction/substitution failed:
test.cpp:50:57: note:   cannot convert ‘3’ (type ‘int’) to type ‘const key_type& {aka const std::__cxx11::basic_string<char>&}’
         set<int> ValueSet = jdEmployees.value(3, nullSet);
                                                         ^
In file included from test.cpp:2:0:
nlohmann/json.hpp:20301:14: note: candidate: nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::string_t nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::value(const typename nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::object_t::key_type&, const char*) const [with ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>; nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::string_t = std::__cxx11::basic_string<char>; typename nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::object_t::key_type = std::__cxx11::basic_string<char>]
     string_t value(const typename object_t::key_type& key, const char* default_value) const
              ^
nlohmann/json.hpp:20301:14: note:   no known conversion for argument 1 from ‘int’ to ‘const key_type& {aka const std::__cxx11::basic_string<char>&}’
nlohmann/json.hpp:20351:15: note: candidate: template<class ValueType, typename std::enable_if<nlohmann::detail::is_getable<nlohmann::basic_json<>, ValueType>::value, int>::type <anonymous> > ValueType nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::value(const json_pointer&, const ValueType&) const [with ValueType = ValueType; typename std::enable_if<nlohmann::detail::is_getable<nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>, ValueType>::value, int>::type <anonymous> = <enumerator>; ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>]
     ValueType value(const json_pointer& ptr, const ValueType& default_value) const
               ^
nlohmann/json.hpp:20351:15: note:   template argument deduction/substitution failed:
test.cpp:50:57: note:   cannot convert ‘3’ (type ‘int’) to type ‘const json_pointer& {aka const nlohmann::json_pointer<nlohmann::basic_json<> >&}’
         set<int> ValueSet = jdEmployees.value(3, nullSet);
                                                         ^
In file included from test.cpp:2:0:
nlohmann/json.hpp:20375:14: note: candidate: nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::string_t nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::value(const json_pointer&, const char*) const [with ObjectType = std::map; ArrayType = std::vector; StringType = std::__cxx11::basic_string<char>; BooleanType = bool; NumberIntegerType = long int; NumberUnsignedType = long unsigned int; NumberFloatType = double; AllocatorType = std::allocator; JSONSerializer = nlohmann::adl_serializer; BinaryType = std::vector<unsigned char>; nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::string_t = std::__cxx11::basic_string<char>; nlohmann::basic_json<ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType>::json_pointer = nlohmann::json_pointer<nlohmann::basic_json<> >]
     string_t value(const json_pointer& ptr, const char* default_value) const
              ^
nlohmann/json.hpp:20375:14: note:   no known conversion for argument 1 from ‘int’ to ‘const json_pointer& {aka const nlohmann::json_pointer<nlohmann::basic_json<> >&}’
@venkatpvc24
Copy link

venkatpvc24 commented Mar 15, 2021

leonardltk,

I have just read the docs for json, there is no member function value, that takes a container as seocnd parameter.

value methods

set<int> ValueSet = jdEmployees.value(3, nullSet); // you have to change this line to below

set<int> ValueSet = jdEmployees.value(3, " a default value that you want return, if key not found" );

@nlohmann
Copy link
Owner

Please have a look at the documentation and examples of the value function. It is only applicable to JSON objects.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Mar 24, 2021
@leonardltk
Copy link
Author

set<int> ValueSet = jdEmployees.value(3, " a default value that you want return, if key not found" );

a default value has to be an integer? so it means i cannot define set<int> ValueSet, but rather auto ValueSet ?

@nlohmann
Copy link
Owner

nlohmann commented Mar 27, 2021

value takes an object key as first parameter and can only be called on JSON objects. The second parameter must be a JSON value - you cannot pass a std::set<int> there.

Example:

#include <iostream>
#include <set>
#include "json.hpp"

using json = nlohmann::json;

int main()
{
    json j;
    
    // create a JSON object and store an array with 4 integers at key "key"
    j["key"] = {1,2,3,4};
    
    // access call via []
    std::set<int> s1 = j["key"];
    
    // access via value
    std::set<int> nullset;
    std::set<int> s2 = j.value("key", json(nullset));
    std::set<int> s3 = j.value("nonexisting", json(nullset));
}

@nlohmann
Copy link
Owner

Do you need further assistance with this issue?

@nlohmann nlohmann closed this as completed May 4, 2021
@blandrum2
Copy link

I am doing a simple assignment data["Name"] = { 'W', 'O', '1' }; in Visual Studio 2022. It won't compile, I get the error code C2678, because of the error: "No operator "=" matches these operands.

I used this line: using json = nlohmann::json;. To include the namespace.

The declaration statement for data is:

nlohmann::json data;

I also tried another line of code that has worked for other coders:

data["key"] = { 1, 2, 3 };

This too generates the same error.

Why am I seeing this error?

@gregmarr
Copy link
Contributor

@blandrum2 That's because there is no assignment operator on basic_json that takes an initializer_list. You need to construct the sub-object first and then assign it, such as by wrapping it as data["key"] = json({1,2,3}).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs more info the author of the issue needs to provide more details
Projects
None yet
Development

No branches or pull requests

5 participants