簡體   English   中英

錯誤左值必須為一元'&'操作數

[英]Error lvalue required as unary '&' operand

在對象中創建線程時遇到問題。 錯誤是需要左值作為一元'&'操作數

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM