2017-01-07 31 views
1

我正在做一些代碼,我將輸出重定向到一個文件,但我得到錯誤使用fstream,ostream,streambuf()。它說,C++錯誤保存到文件使用流

- 「fstream的」 不明確

- 「ostream的」 不明確

-class 「的std :: basic_ostream>」 沒有成員 「流緩衝」

-class「STD :: shared_ptr「沒有會員」關閉「

我已經搜索它的包括,安裝最新版本的卡薩布蘭卡的休息api,...我仍然得到那些erros ..其缺失的一些包括?

下面是代碼

#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()); 
    } 
+1

您是否嘗試過使用'std'前綴,如'std :: fstream'? –

+0

是的。如果我在這裏添加std :: fstream:fstream :: open_ostream(U(「results.html」))。然後([=](ostream outFile),我得到以下錯誤:\t class「std :: basic_fstream >「沒有成員」open_ostream「 – Tazz

+1

@Tazz它沒有'open_ostream'成員,請停止猜測並閱讀一些文檔,一本好書也許是個好主意, – molbdnilo

回答

1

-"fstream" is ambiguous

-"ostream" is ambiguous

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

您衝突std::和卡薩布蘭卡的concurrency::streams命名空間,無論是確保他們從來沒有在一個文件中與using拉或使用concurrency::streams明確

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

但它不!您fileStream


編輯使用->:我覺得你的代碼是什麼,但官方sample稍作修改的版本,你可以在你得到它只是仔細檢查權