c++ error: invalid use of non-static member function

      在把一个C代码的编码示例代码封装成一个C++的类,其中涉及到类函数创建一个进程,进程处理函数据是本类的方法函数,遇到了无效使用非静态成员函数的报错。用以下方法解决了报错,同事说还可以用单例模式来解决。

#include <atomic>
#include <chrono>
#include <iostream>
#include <string>
#include <sys/epoll.h>
#include <thread>
#include <vector>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>        /* read */

using namespace std;

class AcEncoder
{
    public:
        void StreamingLoop( int x );
        void EncoderYUV();
};

void AcEncoder::StreamingLoop( int x )
{
    while (true)
    {
        sleep(x);
        std::cout << "yuv->h264" << std::endl;
    }
}

void AcEncoder::EncoderYUV()
{
    int second = 2;

    /*
     * error: invalid use of non-static member function ‘void AcEncoder::StreamingLoop(int)’
     * 无效使用非静态成员函数
     *
     * 报错原因:因为没有类对象,也就是没有类的实例化
     */

    // std::thread enc1( StreamingLoop, second );     /* error */
                                
    /*
     * 在 C++ 中,成员函数有一个隐含的第一个参数绑定(bind)到 this。创建线程时,必须传递this指针。
     * 还必须使用类名来限定成员函数。
     *
     * 这种情况下正确的线程构造函数如下所示:
     */
    
    std::thread enc1( &AcEncoder::StreamingLoop, this, second );


    /*
     * 也可以将 lambda 传递给线程构造函数:
     */
    std::thread enc2([this, second] // capture the this pointer and second by value
    {
        this->StreamingLoop(second);
    });

    enc1.join();
    enc2.join();
}

int main()
{
    AcEncoder en;

    en.EncoderYUV();

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 错误:无效使用非静态成员函数。 这个错误通常发生在尝试在静态函数或静态上下文中使用非静态成员函数时。非静态成员函数是依赖于类的实例的,而静态函数或静态上下文是独立于类的实例的。因此,不能在静态函数或静态上下文中直接使用非静态成员函数。 要解决这个错误,可以将非静态成员函数改为静态成员函数,或者在静态函数或静态上下文中使用类的实例来调用非静态成员函数。 ### 回答2: error: invalid use of non-static member function(错误:使用非静态成员函数无效),这个错误通常发生在类的成员函数中。在C++中,如果一个成员函数没有被声明为静态,那么它就是一个非静态成员函数,它是依赖于对象的,只有通过对象来调用。 这个错误的原因是在使用非静态成员函数时,没有正确地使用它。在使用非静态成员函数时,需要使用对象来调用,而不是使用类名或指针来调用。如果直接使用类名或指针调用非静态成员函数,就会引起这个错误。 例如,下面这段代码就会产生这个错误: ``` class MyClass { public: void myFunc() { /* do something */ } }; int main() { MyClass::myFunc(); // invalid use of non-static member function return 0; } ``` 我们可以看到,我们试图使用类名来调用myFunc()函数,但是这个函数是一个非静态成员函数,它需要通过对象来调用。我们需要创建一个MyClass对象,并调用它的myFunc()函数: ``` int main() { MyClass obj; obj.myFunc(); // correct usage of non-static member function return 0; } ``` 在这个例子中,我们创建了一个MyClass对象obj,并使用它来调用myFunc()函数,这样就可以正确地使用非静态成员函数了。 总之,当编译器提示error: invalid use of non-static member function时,我们需要检查代码中是否正确地使用了非静态成员函数,如果直接使用类名或指针来调用,就需要使用对象来调用。 ### 回答3: 在C++中,如果我们想在一个类的成员函数或者非成员函数中访问该类的属性或者调用类的方法,我们需要使用“.”来访问成员,或者使用“->”来访问指向该类的指针的成员。但如果我们在非成员函数中直接访问类的成员函数,就会出现“error: invalid use of non-static member function”的错误。 这个错误的原因是,C++中的非静态成员函数只能在对象创建后才能使用。每个对象都有自己独特的非静态成员函数,因此不能像静态成员函数一样直接访问。 但是,我们可以通过将非成员函数声明为friend函数来解决这个问题。这样做不仅可以避免“error: invalid use of non-static member function”的错误,还可以使该函数访问类的私有成员。 总之,“error: invalid use of non-static member function”通常是因为在非成员函数中使用非静态成员函数而引起的。我们应该使用正确的语法来访问类的成员函数,或者使用friend函数来解决这个问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值