在Object中创建线程时遇到问题。错误是左值作为一元'&'操作数
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(&amp; AirQ :: processDataAQ(),this); ,我不明白出了什么问题。
我主要是这样创建对象。
AirQ* test = new AirQ(0,true);
任何帮助将不胜感激。
解决方案: 将&AirQ :: processDataAQ()更改为&amp; AirQ :: processDataAQ。后者是指向成员的指针函数。 - Pete Becker
答案 0 :(得分:1)
std::thread air = std::thread(&AirQ::processDataAQ(), this);
// ^^
括号调用该函数。你不想调用这个函数;你只想给它命名。
删除括号。
我还建议摆脱复制初始化。
所以,简单地说:
std::thread air(&AirQ::processDataAQ, this);