Ryan
Ryan

Reputation: 10099

Trying to make std::make_pair take more than 2 arguments

I was previously returning 2 items in my return statement, but i had to modify it so that i could return 3 things, I have made some changes to the code, this is what looks like

std::pair<int, std::pair<std::string, std::string>> get(InputValidator inputValidator){
    std::string imageURL = inputValidator.url();

    Requests requests;
    cv::Mat image = requests.downloadImageFromURL(imageURL);
    if(image.empty())
        return std::make_pair(0,empty, "");

    try{
        std::vector<std::string> elements;
        cv::Mat croppedFI = rotated_f(image);
        std::string returnFP = rotated_F(image);
        elements.push_back(returnFP);
        std::string uploadUrl = requests.uploadImageToURL(APP.uploadURLSilo, cropped);
        elements.push_back(uploadUrl);
        // return elements[0], elements[1];
        return std::make_pair(0, uploadUrl,returnFP);

but this throws an error like

app.cpp: In member function ‘std::pair<int,std::pair<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > > ResponseFunction::get(InputValidator)’:
app.cpp:91:46: error: no matching function for call to ‘make_pair(int, std::__cxx11::string&, const char [1])’
             return std::make_pair(0,empty, "");

The traceback continues but mostly looks like the same error.

My c++ is not that great ,any ideas would be really helpful.Thanks in advance.

Upvotes: 1

Views: 1494

Answers (1)

Max Langhof
Max Langhof

Reputation: 23681

You could fix the error by writing

return std::make_pair(0, std::make_pair(uploadUrl,returnFacePoints));

But std::pair, as the name suggests, is not made for holding three things. std::tuple exists for combining any number of things:

return std::make_tuple(0, uploadUrl, returnFacePoints);

The return type of the function would just be

std::tuple<int, std::string, std::string> get(InputValidator inputValidator){

Do note that this is not exactly best practice in C++. A user of your function has no idea that the first returned std::string is the upload URL and the other one is about the face points - and even if they know, it's very easy to confuse the two. Yes, other languages do this too, but they tend to have smaller code bases.

Upvotes: 12

Related Questions