0

I have a make() static method in a class Response. It is overloaded to take either std::string or Json::Value from jsoncpp.

I overloaded the method to look like this:

Response::make(Json::Value body)
{
  ...
}

Response::make(std::string body)
{
  ...
}

This causes this error on compilation:

more than one instance of overloaded function "Response::make" matches the argument list:C/C++(308)
main.cpp(15, 20): function "Response::make(std::string body)" (declared at line 28 of "/home/user/weasle/./weasel/./Response.h")
main.cpp(15, 20): function "Response::make(Json::Value body)" (declared at line 36 of "/home/user/weasle/./weasel/./Response.h")
main.cpp(15, 20): argument types are: (const char [33])

Is Json::value treated as a string? How can I fix this issue?

4
  • Let's see how you are calling this function from main. -- How do I correctly overloaded methods -- The issue probably has nothing to do with this. It may have everything to do with how you are calling the function. Commented Dec 10, 2023 at 23:59
  • Be kind to post a minimal reproducible example. Response::make(Json::Value body) is an invalid C++ code. Commented Dec 11, 2023 at 0:00
  • 1
    The error message indicates that you are trying to call make with some string literal as argument. Which of the two overloads do you expect to be used for this? Both can make sense. So maybe overloading isn't the correct approach in this case at all. Commented Dec 11, 2023 at 0:13
  • According to documentation (on both sourceforge and github - I'm not sure which is definitive) Jason::Value has a constructor that accepts a const char *. So does std::string. Calling some_response.make("string literal") will mean that conversions to std::string and Json::Value are both equally viable, hence the ambiguity. You need to either remove or rename one of your overloads of make(), or force the conversion e.g. some_response.make(std::string("string literal")) or some_response.make(Json::Value("string literal")) to remove the ambiguity at the call site. Commented Dec 11, 2023 at 0:17

1 Answer 1

7

According to the error message, you are passing a const char[33] array (I'm assuming a string literal?) to make(). But, you don't have an overload that accepts that type, so the compiler has to convert it to another type that you do have an overload for.

However, both std::string and JSON::Value have a constructor that accepts a const char* (which a const char[] decays into), so the compiler doesn't know which overload it should call, hence the error.

So, you have 2 choices:

  • add a third overload that takes a const char* as input:

    Response::make(const char* body)
    {
      ...
    }
    
    Response::make(Json::Value body)
    {
      ...
    }
    
    Response::make(std::string body)
    {
      ...
    }
    
  • explicitly do the desired conversion yourself at the call site, eg:

    Response::make(Json::Value("..."));
    
    Response::make(std::string("..."));
    

    Alternatively, for std::string in C++14 and later:

    using namespace std::literals;
    Response::make("..."s);
    
Sign up to request clarification or add additional context in comments.

1 Comment

Or a less verbose version of the last: "..."s if you are using std::literals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.