繁体   English   中英

<<运算符抛出编译错误

[英]<< Operator throwing compile error

我正在关注下面的基本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;
}

我用了c ++已经有一段时间了,但我确信我以前使用过<<运算符。 我错过了使其成功的包含吗?

你不能像这样重定向标准输出:为了使<<运算符工作, myRequest.perform()成员函数需要返回它的输出 - 作为string ,或作为另一个存在重载的对象<<输出流的运算符。

由于myRequest.perform()为void,因此您需要告诉curlpp使用其他一些机制来写入字符串流。 在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