[英]invalid use of non-static member function C++ while passing member-function as a function variable
I know similar questions have been asked, but I can't point my finger on my issue. 我知道有人问过类似的问题,但我不能指责我的问题。 I have a class that has the following method (it is used for multi-threaded programming):
我有一个具有以下方法的类(用于多线程编程):
void ServerUtil::WaitForRequst(void (*ProcessRequest)(shared_ptr<TCP> tcp), shared_ptr<struct sockaddr_in> serv_addr){
TCPPort tcp_port(serv_addr, true);
struct sockaddr_in client_addr; //FIXME: add these to a vector so their is a way to track clients
int i = 0;
while(i < m_thread_max){
std::shared_ptr<TCP> tcp = std::make_shared<TCP>(serv_addr, &client_addr, tcp_port, true);
std::shared_ptr<std::thread> t = std::make_shared<std::thread>(std::bind(ProcessRequest, tcp));;
m_vector_threads.push_back(t);
i++;
}
for(int j = 0; j < m_vector_threads.size();j++)
m_vector_threads.at(j)->join();
}
}
Now I've created a server class with the following .h file: 现在,我用以下.h文件创建了一个服务器类:
class Server{ public: 服务器类{公共:
Server(shared_ptr<ServerUtil> serv_util, shared_ptr<struct sockaddr_in> serv_addr);
virtual void ProcessCommand(char &command) = 0;
//ProcessRequest is the primary thread(client) handler
virtual void ProcessRequest(shared_ptr<TCP> tcp) = 0;
bool SendAck(shared_ptr<TCP> tcp);
bool ReceiveAck(shared_ptr<TCP> tcp);
bool CloseConnection(shared_ptr<TCP> tcp);
protected:
shared_ptr<ServerUtil> m_serv_util;
shared_ptr<struct sockaddr_in> m_serv_addr;
}; };
And now the following .cpp file for the issue in question: 现在,该问题的以下.cpp文件:
Server::Server(shared_ptr<ServerUtil> serv_util, shared_ptr<struct sockaddr_in> serv_addr){
m_serv_util = serv_util;
m_serv_addr = serv_addr;
m_serv_util->WaitForRequst(ProcessRequest, serv_addr);
}
And I get the following error: 我得到以下错误:
error: invalid use of non-static member function
m_serv_util->WaitForRequst(ProcessRequest, m_serv_addr);
I also tried commenting out the constructor in the base class and doing this in a derived class (With the pure virtual functions overwritten). 我还尝试注释掉基类中的构造函数,并在派生类中这样做(覆盖了纯虚函数)。 Any ideas?
有任何想法吗?
In 在
void ServerUtil::WaitForRequst(void (*ProcessRequest)(shared_ptr<TCP> tcp),
shared_ptr<struct sockaddr_in> serv_addr)
void (*ProcessRequest)(shared_ptr<TCP> tcp)
specifies that the function will be called with a pointer to a function that matches the given void (shared_ptr<TCP> tcp)
prototype. void (*ProcessRequest)(shared_ptr<TCP> tcp)
指定将使用指向与给定void (shared_ptr<TCP> tcp)
原型相匹配的函数的指针来调用该函数。
void ProcessRequest(shared_ptr<TCP> tcp)
is a member of Server
so it really looks more like 是
Server
的成员,所以它看起来实际上更像
void ProcessRequest(Server * this, shared_ptr<TCP> tcp)
You can sort-of clean that up with 您可以使用以下方法进行清理
void ServerUtil::WaitForRequst(void (Server::*ProcessRequest)(shared_ptr<TCP> tcp),
shared_ptr<struct sockaddr_in> serv_addr)
to let WaitForRequst
(aside: you have a typo there than may bite you later) know that it will be getting a method of Server
. 让
WaitForRequst
( WaitForRequst
一句:那里有错别字,以后可能会咬人)知道它将得到Server
的方法。 But...! 但...! you will also have to pass in an object instance for
this
, so 你也将有一个对象实例通过
this
,所以
void ServerUtil::WaitForRequst(void (Server::*ProcessRequest)(shared_ptr<TCP> tcp),
Server * instance,
shared_ptr<struct sockaddr_in> serv_addr)
And this instance needs to be handed to the thread with something like 这个实例需要用类似的东西传递给线程
std::shared_ptr<std::thread> t = std::make_shared<std::thread>(std::bind(ProcessRequest,
instance,
tcp));
std::thread
will solve the messiness involved with invoking a method pointer for you, but if you are interested, here's a big long discussion on the trials and tribulations of method pointers: Pointers to Member Functions . std::thread
将为您解决涉及调用方法指针的麻烦问题,但是,如果您有兴趣,这里将进行长时间的讨论,讨论方法指针的试验和麻烦: 成员函数的指针 。
Unfortunately you are doing stuff there that I think deserves another question. 不幸的是,您正在那里做一些我认为值得另一个问题的事情。 You do not want those
shared_ptr
s. 您不需要这些
shared_ptr
。 No one is sharing the pointers, so std::unique_ptr
or no pointer at all should be the go-to. 没有人共享指针,因此
std::unique_ptr
或完全没有指针应该成为std::unique_ptr
。 No pointer at all looks to be good here, with the possible exception of instance
(and consider a re-design if the scope of instance is unreliable or shorter than the thread). 除了
instance
可能的例外,这里没有任何指针看起来是好的(如果实例的范围不可靠或比线程短,请考虑重新设计)。
That means 那意味着
void ProcessRequest(TCP tcp)
the obligatory 强制性的
void ServerUtil::WaitForRequst(void (Server::*ProcessRequest)(TCP tcp),
Server * instance,
shared_ptr<struct sockaddr_in> serv_addr)
and 和
m_vector_threads.emplace_back(ProcessRequest,
instance,
TCP(serv_addr,
&client_addr,
tcp_port,
true));
may be sufficient, depending on how well-written TCP
is. 可能就足够了,这取决于
TCP
的编写方式。 It might be better to pass in all of the parameters and have ProcessRequest
construct the TCP instance as a local variable. 最好传入所有参数,并让
ProcessRequest
将TCP实例构造为局部变量。 Forgive me if there is a minor syntax error in there. 如果其中存在较小的语法错误,请原谅我。 It's not like I have enough to test all this out.
好像我没有足够的能力来测试所有这些。 Feel free to kick my ass for major syntax errors, though.
不过,如果遇到重大语法错误,请随意打我的屁股。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.