「 C++ 11」std::thread “invalid use of non-static member function“问题处理

本文探讨了在C++中如何处理'invalid use of non-static member function'错误,通过实例演示了如何使用类成员函数指针和静态成员函数来启动线程。适合初学者理解线程编程的最佳实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


🟠问题简述:

项目中使用std::thread把类的成员函数作为线程函数,在编译的时候报错了:"invalid use of non-static member function",于是研究了一番,于是就产生了这篇博文,记录一下。

错误示例

#include <iostream>
#include <thread>
#include <stdlib.h>

using namespace std;

class Test
{
public:
    void testDemo()
    {
        cout << "hello world." << endl;
    };
};


int main()
{
    Test myTest;
    std::thread t(myTest.testDemo);
	t.join();
    return 0;
}

问题展示

main.cpp: In function ‘int main()’:
main.cpp:31:26: error: invalid use of non-static member function ‘void Test::testDemo()’
   31 |     std::thread t(myTest.testDemo);
      |                   ~~~~~~~^~~~~~~~
main.cpp:18:10: note: declared here
   18 |     void testDemo()
      |     

🟡处理思路:

🔴 方法一

根据std::thread构造函数进行传参。

  • 类成员函数的指针
    当std::thread内部创建新线程时,它将使用传入的成员函数作为线程函数。
  • 类的指针
    在每个非静态成员函数中,第一个参数始终是指向其自身类对象的指针。因此,线程类将在调用传递的成员函数时将这个指针作为第一个参数传递。

🟢C++代码:

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <thread>
#include <stdlib.h>

using namespace std;

class Test
{
public:
    void testDemo()
    {
        cout << "hello world." << endl;
    };
};

int main()
{
    Test myTest;
    std::thread t(&Test::testDemo, &myTest);
	t.join();
    return 0;
}

🔵结果展示:

hello world.

🔴 方法二

把线程函数修改为静态成员函数
因为静态函数不与类的任何对象相关联。因此,我们可以直接将类的静态成员函数作为线程函数传递,而无需传递任何指向对象的指针。

🟢C++代码:

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <iostream>
#include <thread>
#include <stdlib.h>

using namespace std;

class Test
{
public:
    static void testDemo()
    {
        cout << "hello world." << endl;
    };
};

int main()
{
    Test myTest;
    std::thread t(myTest.testDemo);
    t.join();
    return 0;
}

🔵结果展示:

hello world.

🟣引经据典:

https://thispointer.com/c11-start-thread-by-member-function-with-arguments/
https://cplusplus.com/reference/thread/thread/

评论 52
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谁吃薄荷糖

你获得知识,我获得财富,双赢!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值