c++ - 错误左值需要作为一元 '&' 操作数

标签 c++ multithreading intel-edison

我在对象中创建线程时遇到问题。错误是需要作为一元“&”操作数的左值

CPP文件

#include "AirQ.h"


static int i=0;


AirQ::AirQ(int pinNo, bool input):Sensor("AIRQUALITY", "PPM",pinNo,input) {
    threadShouldRunAQ = true;
    this->bufferLength = 256;

        signal(SIGINT, AirQ::signalHandler);
        airQualitySensor = new upm::Ublox6(pinNo);

        if (!airQualitySensor->setupTty(B9600))
            std::cerr << "[ERROR][GPS] Failed to setup tty port parameters!" << std::endl;

        try
        {
            std::thread air = std::thread(&AirQ::processDataAQ(), this);
        }
        catch (std::exception e)
        {
            std::cerr << "[ERROR][GPS] " << e.what() << std::endl;
        }
}

AirQ::~AirQ() {
    // TODO Auto-generated destructor stub
}


void AirQ::signalHandler(int sigNo)
{
    if (sigNo == SIGINT)
        threadShouldRunAQ = false;

}

void AirQ::processDataAQ()
{

    while (threadShouldRunAQ)
    {
        i++;
        if (airQualitySensor != NULL)
            if (airQualitySensor->dataAvailable())
            {
                //TODO
            }


            usleep(100000);
    }
}

void AirQ::getData(std::string value)
{
    this->readBuffer = value;
}

std::string AirQ::logData()
{
    AirQ::setCollectedFlag(false);
    return this->readBuffer;
}

void AirQ::setCollectedFlag(bool flag)
{
    this->collectedFlag = flag;
}

H文件

#include <ublox6.h>
#include "Sensor.h"

#ifndef AIRQ_H_
#define AIRQ_H_

static bool threadShouldRunAQ;
static upm::Ublox6* airQualitySensor;



class AirQ: private Sensor {
private:
    std::string readBuffer;
    bool collectedFlag;
    size_t bufferLength;
    static void signalHandler(int);
    void processDataAQ();

protected:


public:
    AirQ(int, bool);
    virtual ~AirQ();

    std::string logData();
    void getData(std::string);
    void setCollectedFlag(bool);
    std::thread processingThread;
};


#endif /* AIRQ_H_ */

CPP文件报错,行 std::thread air = std::thread(&AirQ::processDataAQ(), this); 我不明白哪里出了问题。

我主要是像这样创建对象。

AirQ* test = new AirQ(0,true);

我们将不胜感激。

解决方案: 将 &AirQ::processDataAQ() 更改为 &AirQ::processDataAQ。后者是指向成员函数的指针。 – 皮特·贝克尔

最佳答案

std::thread air = std::thread(&AirQ::processDataAQ(), this);
//                                                ^^

括号调用函数。您不想调用该函数;你只想给它命名。

删除括号。


我还建议摆脱复制初始化。

所以,简单地说:

std::thread air(&AirQ::processDataAQ, this);

关于c++ - 错误左值需要作为一元 '&' 操作数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36814214/

相关文章:

c++ - 在 Win32 C++ 代码中的哪个位置放置 SetMenuItemInfo() 以根据焦点启用/禁用?

java - 在用户输入java上暂停线程池

java - 使用 TaskExecutor 时出现 AssertionError

c - 需要 "hello world"才能用 C 语言写入 Intel Edison gpio

c - 如何在 C 中使用 "popen"写入和读取同一文件

c++ - 线程工作目录

c++ - C++基于成员函数从 vector 中删除对象

c++ - 在 Win32 GUI 中的按钮上使用 ↺

java - Log4j2 - 查找插件 (StrLookup) 解析 ThreadName 以通过 Thread 路由 RollingLogFile

C++ Eclipse 控制台将纯文本编码为其他字符