错误左值需要作为一元“&”操作数
Error lvalue required as unary '&' operand
我在对象中创建线程时遇到问题。错误是 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);
我在对象中创建线程时遇到问题。错误是 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);