[英]<< 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.