简体   繁体   中英

<< Operator throwing compile error

I am following the basic libcurl curlcpp example below

#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

#include <string>
#include <sstream>
#include <iostream>

// RAII cleanup 
curlpp::Cleanup myCleanup;

// standard request object.
curlpp::Easy myRequest;

int main(int, char**)
{
    // Set the URL.
    myRequest.setOpt(new curlpp::options::Url(std::string("http://www.wikipedia.com")));

    // Send request and get a result.
    // By default the result goes to standard output.
    // Here I use a shortcut to get it in a string stream ...
    std::ostringstream os;
    os << myRequest.perform();

    std::string asAskedInQuestion = os.str();

    return 0;
}

It has been a while since I used c++ but I am sure I have used the << operator before. Am I missing an include needed to make it work?

You cannot redirect standard output like that: in order for the << operator to work, the myRequest.perform() member function needs to return its output - either as a string , or as another object for which there exists an overload of the << operator for output streams.

Since myRequest.perform() is void, you need to tell curlpp to write to your string stream by using some other mechanism. In curlpp that is done by setting a write stream option - like this:

std::ostringstream os;   // Here is your output stream
curlpp::options::WriteStream ws(&os);
myRequest.setOpt(ws);    // Give it to your request
myRequest.perform();     // This will output to os

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM