在类中创建线程会导致错误C2664

Making a thread inside a class give me error C2664

本文关键字:错误 C2664 线程 创建      更新时间:2023-10-16

让我把你放在上下文中。我正在使用Qt Creator制作一个聊天框。我对C++还很陌生,所以这是一个挑战。我想使用std::thread创建一个线程。这是我写的代码:

window.h

class Window: public QWidget
{
Q_OBJECT
public:
Window();
public slots:
    void Config();
private:
    void ListenToClients(Server server);
    //member...
};

window.cpp

Window::Window(): QWidget()
{
    //not important code...    
    do
    {
        //nothing
    }while(config.getLaunch() == false);
    Server myServer(config.getPort(), config.getBroadcast());
    std::thread serverThread(&Window::ListenToClients, this, myServer);
}
void Window::Config()
{
    config.exec();
}
void Window::ListenToClients(Server server)
{
    for(int i = 0; i < 100; i++)
    {
        server.ListenNewConnections();
    }
}

所以我得到了这个错误:错误:C2664:'std::tuple<void (__cdecl Window::* )(Server),Window *,Server>::tuple(std::tuple<void (__cdecl Window::* )(Server),Window *,Server> &&)'ÿ: imossible to convert argument 1 of 'void (__cdecl Window::* )(Server)' in 'std::allocator_arg_t'

我不知道为什么,也不知道这意味着什么。我搜索了一下,但似乎没有人出现过这样的错误。所以,我指望你们所有人。

"std::allocater_arg_t"听起来像是您的构造函数参与其中,因为它正在分配一些东西:

Window::Window(): QWidget()无效,您已经在类分离中继承了QWidget,因此QWidget的构造函数被隐式调用。CCD_ 4就足够了。

另外:我不知道public slots:应该是什么意思。您可以完全省略它,因为您已经有了上面的public:限定符。