我正在使用wxWidgets,但我认为这对我的问题没有区别。
问题是我需要在threadWaitConnection :: Entry中初始化threadReadPacket时复制并保存在socketConnect内存的不同位置。 我正在尝试通过构造函数传递此值,但我不能。
有些返回错误消息:
main.cpp:97:70: error: within this context
threadReadPacket *thread = new threadReadPacket(socketConnect);
^
main.cpp:22:5: error: initializing argument 1 of ‘threadReadPacket::threadReadPacket(wxSocketBase)’
threadReadPacket(wxSocketBase setSocket) {
^
代码:
/* Thread */
// threadReadPacket
class threadReadPacket : public wxThread {
public:
threadReadPacket(wxSocketBase setSocket) {
socket = setSocket;
};
virtual ~threadReadPacket();
virtual void *Entry();
private:
wxSocketBase socket;
};
threadReadPacket::~threadReadPacket() {
}
threadReadPacket::ExitCode threadReadPacket::Entry() {
/* [...]
Lots and lots of lines of code
[...] */
}
// threadWaitConnection
class threadWaitConnection : public wxThread {
public:
threadWaitConnection(wxSocketServer *setSocket) {
socket = setSocket;
};
virtual ~threadWaitConnection();
virtual void *Entry();
private:
wxSocketServer *socket;
};
threadWaitConnection::~threadWaitConnection() {
}
threadWaitConnection::ExitCode threadWaitConnection::Entry() {
wxPrintf("Waiting for connection...\n");
wxSocketBase socketConnect;
socket->AcceptWith(socketConnect, true);
if (socketConnect.Ok()) {
wxPrintf("Success on connect\n");
threadReadPacket *thread = new threadReadPacket(socketConnect);
if (thread->Run() != wxTHREAD_NO_ERROR) {
wxPrintf("Can't start thread!");
return NULL;
}
} else {
wxPrintf("Not connected\n");
return NULL;
}
// Finish
socketConnect.Close();
return NULL;
}
答案 0 :(得分:1)
见:
/usr/include/wx-3.0-unofficial/wx/socket.h:303:29: error: ‘wxSocketBase::wxSocketBase(const wxSocketBase&)’ is private
wxDECLARE_NO_COPY_CLASS(wxSocketBase);
这意味着无法复制套接字对象,因为它的复制构造函数是私有的。
您的代码应该保留对套接字对象的引用(或指针)。
答案 1 :(得分:0)
不知道它是否是最优雅的解决方案,但它是唯一有效的解决方案。 我在堆中复制了socket的内存,并将她的位置传递给了构造函数。
这样我可以存储多个套接字并以独立方式使用它们。