0

I have a problem while creating a thread in an Object. Error is lvalue required as unary '&' operand

CPP File

#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 File

#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_ */

The error is reported in CPP File, line std::thread air = std::thread(&AirQ::processDataAQ(), this); and i do not understand what is wrong.

I main i create the object like this.

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

Any help will be appreciated.

Solution: Change &AirQ::processDataAQ() to &AirQ::processDataAQ. The latter is a pointer-to-member-function. – Pete Becker

1 Answers1

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

The brackets call the function. You don't want to call the function; you only want to name it.

Remove the brackets.


I would also suggest getting rid of the copy-initialisation.

So, simply:

std::thread air(&AirQ::processDataAQ, this);
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055