gpt4 book ai didi

c++ - Boost noncopyable is private 错误

转载 作者:搜寻专家 更新时间:2023-10-31 00:39:07 25 4
gpt4 key购买 nike

首先,我在我的 Windows 机器上运行了完全相同的代码,而且我不经常使用 Linux,所以我尝试编译它的方式可能存在问题。我将不胜感激任何帮助找出我遇到的错误,因为我不确定是什么原因造成的,并且通过文件树会返回到 io_service.hpp,这不应该有问题(如果我正在做没错)。

g++ -L/usr/include/ -lboost_regex Source.cpp -o source

我的client.cpp代码

#include "client.h"

std::string port = "13"; /* Destination port. */

void client::write()
{
using namespace std; // For strlen.
std::cout << "Enter message: ";
char request[max_length];
std::cin.getline(request, max_length);
size_t request_length = strlen(request);
boost::asio::write(*s, boost::asio::buffer(request, request_length));
}

void client::send(char c[])
{
boost::asio::write(*s, boost::asio::buffer(c, max_length));
}

// This will be used by the edge nodes to connect to the server
client::client(void)
{
tcp::resolver resolver(io);
tcp::resolver::query query(tcp::v4(), "ALERT-Core-0", port);
tcp::resolver::iterator iterator = resolver.resolve(query);

s = new tcp::socket(io);
s->connect(*iterator);
}

客户端.h

#pragma once
#include <boost\asio.hpp>
#include <boost\thread.hpp>

using namespace boost::asio::ip;

class client
{
public:
client(void);
~client(void) { }
void write();
void send(char c[]);

private:
boost::asio::io_service io;
tcp::socket* s;
static const size_t max_length = 1024;
};

我的主函数通过调用初始化它

client c = client();
while(s[0] != '1')
{
c.send(s);
memset(&s, '\0', 1024);
std::cin.getline(s, 1024);
}

终端的输出是:

In file included from /usr/include/boost/asio/basic_io_object.hpp:20:0,
from /usr/include/boost/asio/basic_socket.hpp:24,
from /usr/include/boost/asio/basic_datagram_socket.hpp:25,
from /usr/include/boost/asio.hpp:20,
from Client.h:2,
from Source.cpp:1:
/usr/include/boost/noncopyable.hpp: In copy constructor ‘boost::asio::io_service::io_service(const boost::asio::io_service&)’:
/usr/include/boost/noncopyable.hpp:27:7: error: ‘boost::noncopyable_::noncopyable::noncopyable(const boost::noncopyable_::noncopyable&)’ is private
/usr/include/boost/asio/io_service.hpp:179:1: error: within this context
In file included from Source.cpp:1:0:
Client.h: In copy constructor ‘client::client(const client&)’:
Client.h:11:1: note: synthesized method ‘boost::asio::io_service::io_service(const boost::asio::io_service&)’ first required here
Source.cpp: In function ‘int main()’:
Source.cpp:5:20: note: synthesized method ‘client::client(const client&)’ first required here

最佳答案

您正在使用 client c = client(); 这行调用类的默认复制构造函数,而这又试图复制类的 io 成员,但是io_service 被标记为不可复制,因此失败了。您可以通过将代码更改为简单的 client c; 来修复它。

由于您的类具有不可复制的成员,您应该自己将复制构造函数(和复制赋值运算符)设为私有(private)以使失败更加明显。

至于它在某些编译器上没有失败的原因,可能是您在 Windows 上使用的任何编译器都可以看到 client c = client(); 在语义上确实与 client c; 并正在为您进行更改,但我不知道标准是否要求甚至允许这样做(或者是一个好主意)。

关于c++ - Boost noncopyable is private 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16821457/

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