繁体   English   中英

无效使用非静态成员函数(在qt中)

[英]invalid use of non-static member function( in qt)

我已经在我的mainwindow.h类文件中创建了函数原型(header?):

    class MainWindow : public QMainWindow
{
    Q_OBJECT

    public:
    void receiveP();

然后在我的main.cpp类文件中,告诉函数该怎么做:

void MainWindow::receiveP()
{
     dostuff
}

然后在main.cpp类文件的main函数中,尝试在线程中使用它:

 std::thread t1(MainWindow::receiveP);
 t1.detach();

这给了我错误“无效使用非静态成员函数'void MainWindow :: receiveP()'。

您正在尝试将成员函数指针传递给thread类的构造函数,该类需要一个普通的(非成员)函数指针。

而是传递静态方法函数指针(或指向自由函数的指针),并显式为其提供对象的实例:

// Header:
static void receivePWrapper(MainWindow* window);

// Implementation:
void MainWindow::receivePWrapper(MainWindow* window)
{
    window->receiveP();
}

// Usage:
MainWindow* window = this;   // Or whatever the target window object is
std::thread t1(&MainWindow::receivePWrapper, window);
t1.detach();

销毁窗口对象之前 ,请确保线程终止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM