[英]Boost noncopyable is private error
首先,我在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);
}
Client.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();
行調用類的默認副本構造函數client c = client();
依次嘗試復制您類的io
成員,但是io_service
被標記為不可復制,因此失敗了。 您可以通過將代碼更改為僅client c;
來解決此問題client c;
。
由於您的類具有不可復制的成員,因此您應該自己將復制構造函數(和復制賦值運算符)設為私有,以使失敗更明顯。
由於某些編譯器沒有失敗的原因,您在Windows上使用的任何編譯器都有可能看到client c = client();
實際上在語義上與client c;
相同client c;
並且正在為您進行更改,但是我不知道標准是否要求或什至允許這樣做(或就此而言是個好主意)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.