简体   繁体   English

<<运算符抛出编译错误

[英]<< Operator throwing compile error

I am following the basic libcurl curlcpp example below 我正在关注下面的基本libcurl curlcpp示例

#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. 我用了c ++已经有一段时间了,但我确信我以前使用过<<运算符。 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. 你不能像这样重定向标准输出:为了使<<运算符工作, myRequest.perform()成员函数需要返回它的输出 - 作为string ,或作为另一个存在重载的对象<<输出流的运算符。

Since myRequest.perform() is void, you need to tell curlpp to write to your string stream by using some other mechanism. 由于myRequest.perform()为void,因此您需要告诉curlpp使用其他一些机制来写入字符串流。 In curlpp that is done by setting a write stream option - like this: 在curlpp中,通过设置写入流选项来完成 - 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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