2016-04-23 7 views
0

Ich habe ein Problem beim Erstellen eines Threads in einem Objekt. Fehler wird lvalue erforderlich als unären '&' OperandFehler lvalue erforderlich als unärer '&' Operand

CPP Datei

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

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

Der Fehler wird in CPP Datei gemeldet, Zeile std :: Gewinde Luft = std :: thread (& AirQ :: processDataAQ(), this); und ich verstehe nicht, was falsch ist.

Ich Hauptsache ich erstelle das Objekt so.

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

Jede Hilfe wird geschätzt.

Lösung: ändern & AirQ :: processDataAQ(), um & AirQ :: processDataAQ. Letzteres ist eine Zeiger-zu-Stab-Funktion. - Pete Becker

+0

@bkVnet Ja, aber das ist, warum es als Referenz & –

+3

Änderung gesendet '& AirQ :: processDataAQ()' auf '& AirQ :: processDataAQ'. Letzteres ist eine Zeiger-zu-Stab-Funktion. –

+0

Es funktioniert @PeteBecker Bitte posten Sie es als Lösung, um es zu akzeptieren :) –

Antwort

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

Die Klammern rufen die Funktion auf. Sie möchten die Funktion nicht aufrufen; du willst es nur benennen.

Die Halterungen entfernen.


Ich würde auch vorschlagen, die Kopie-Initialisierung loszuwerden.

So einfach wie folgt:

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