gpt4 book ai didi

c++使用流保存到文件时出错

转载 作者:行者123 更新时间:2023-11-30 05:18:41 28 4
gpt4 key购买 nike

我正在编写一些将输出重定向到文件的代码,但使用 fstream、ostream、streambuf() 时出现错误。上面写着

-“fstream”有歧义

-“ostream”含糊不清

-class "std::basic_ostream>"没有成员 "streambuf"

-类“std::shared_ptr”没有成员“close”

我已经在其中搜索了 include,安装了最新版本的 casablanca 的 rest api,......但我仍然遇到这些错误......它缺少一些 include?

这是代码

#include <sstream>
#include <iostream>
#include <fstream>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/http_listener.h> // HTTP server
#include <cpprest/json.h> // JSON library
#include <cpprest/uri.h> // URI library
#include <cpprest/ws_client.h> // WebSocket client
#include <cpprest/containerstream.h> // Async streams backed by STL containers
#include <cpprest/interopstream.h> // Bridges for integrating Async streams with STL and WinRT streams
#include <cpprest/rawptrstream.h> // Async streams backed by raw pointer to memory
#include <cpprest/producerconsumerstream.h> // Async streams for producer consumer scenarios


using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams

using namespace web::http::experimental::listener; // HTTP server
using namespace web::experimental::web_sockets::client; // WebSockets client
using namespace web::json;



//Method

auto fileStream = std::make_shared<std::ostream>();

// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile) //Error here on the fstrea and ostream
{
*fileStream = outFile;

// Create http_client to send the request.
http_client client(U("http://localhost:53213"));

// Build request URI and start the request.
uri_builder builder(U("/search"));
builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET, builder.to_string());
})

// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());

// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf()); //Error here on streambuf
})

// Close the file stream.
.then([=](size_t)
{
return fileStream.close(); //Error on close

});

// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}

最佳答案

-"fstream" is ambiguous

-"ostream" is ambiguous

-class "std::basic_ostream>" has no member "streambuf"

你正在冲突 std:: 和 casablanca 的 concurrency::streams 命名空间,要么确保它们永远不会被拉入 using in one文件或显式使用 concurrency::streams

-class "std::shared_ptr" has no member "close"

但事实并非如此!在你的 fileStream

上使用 ->

编辑:我认为您的代码只不过是官方 sample 的略微修改版本, 你可以仔细检查你是否做对了

关于c++使用流保存到文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41525511/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com