gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:53:47 25 4
gpt4 key购买 nike

我正在构建一个用于通过 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/

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