2013-07-14 52 views
1

我正在構建通過局域網發送文件的客戶端/服務器應用程序。這是嚴重的應用程序,當我要獲取文件名時,我的代碼中出現以下錯誤。C++ Boost - 沒有找到需要類型爲'boost :: filesystem :: path'的右側操作數的操作符

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'boost::filesystem::path' (or there is no acceptable conversion) 

#include "stdafx.h" 
#ifdef _WIN32 
# define _WIN32_WINNT 0x0501 
#endif 
#include <boost/asio.hpp> 
#include <boost/array.hpp> 
#include <boost/filesystem.hpp> 
#include <boost/filesystem/path.hpp> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <iostream> 

std::string filename; 
std::string file; 
boost::asio::io_service io_service; 
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 9999); 
boost::asio::ip::tcp::acceptor acceptor(io_service, endpoint); 
boost::asio::ip::tcp::socket sock(io_service); 
boost::array<char, 4096> buffer; 

void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred) { 
    if (!ec || ec == boost::asio::error::eof){ 
     std::string data(buffer.data(), bytes_transferred); 
     if (filename.empty()) { 
      std::istringstream iss(data); 
      std::getline(iss, filename); 
      file = data; 
      file.erase(0, filename.size() + 1); 
      filename = boost::filesystem::path(filename).filename(); 
     } 
     else 
      file += data; 
     if (!ec) 
      boost::asio::async_read(sock, boost::asio::buffer(buffer), read_handler); 
     else { 
     //code 
    } 
} 

//代碼

+0

filename = boost :: filesystem :: path(filename).filename();你是那個文件名()不是一個路徑...看看你是否可以將其轉換爲字符串。 – alexbuisson

回答

3

只是改變這一行:

filename = boost::filesystem::path(filename).filename(); 

這樣:

filename = boost::filesystem::path(filename).filename().string(); 

基本上,編譯器會提醒您std::string沒有定義任何賦值運算符這需要boost::filesystem::path作爲參數(或者不存在轉換它可以提供一個可以用作賦值運算符參數的類型)。幸運的是,boost::filesystem::path提供了一個返回字符串的函數!

相關問題