C++ Boost - 没有找到接受类型为 'boost::filesystem::path' 的右手操作数的运算符

标签 c++ boost boost-asio lan

我正在构建一个用于通过 LAN 发送文件的客户端/服务器应用程序。这是服务器应用程序,当我要获取文件名时,我的代码出现以下错误。

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
    } 
} 

//代码

最佳答案

只需更改这一行:

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

为此:

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

基本上,编译器告诉您 std::string 没有定义任何将 boost::filesystem::path 作为参数(或它无法进行转换以提供可用作赋值运算符参数的类型)。幸运的是,boost::filesystem::path 提供了一个返回字符串的函数!

关于C++ Boost - 没有找到接受类型为 'boost::filesystem::path' 的右手操作数的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17637021/

相关文章:

c++ - 在不关闭连接的情况下循环 boost asio加密

c++ - 访问数组元素

c++ - 在 C++ 中使用 union 是一种好习惯吗?

c++ - unsigned char std::vector 到 unsigned char[]?

c++ - boost 的 fatal error

c++ - 为什么 std::stoi 和 std::array 不能用 g++ c++11 编译?

c++通用编译时for循环

c++ - boost::regex 和 std::regex 之间不一致?

c++ - 如何在 C++ 的 .json 文件中保存 HTTP GET 的 JSON 响应?

c++ - 如何使 Boost::asio SSL 服务器同时接受 TLS 1.1 和 TLS 1.2?