如何解决C ++字符串未在类中连接
我尝试编写一个小程序来控制与树莓派3B +上的两个ds18b20温度传感器相连的泵和阀门。我为泵定义了一个类别,为阀门定义了一个类别,将来会添加更多的泵和阀门,我还为ds18b20传感器创建了一个类别,该传感器通过1条导线连接。传感器可以通过“文件”读出,因此存在一个基本目录string baseDir = "/sys/bus/w1/devices/";
,后跟设备ID string tSensor1 = "28-3c01a81688f4";
和温度“ file” string tempFile = "/w1_slave";
,完整路径应如下所示:这个/sys/bus/w1/devices/28-3c01a81688f4/w1_slave
。
我得到的是这个TestSensor1= Error reading file at /sys/bus/w1/devices//w1_slave
设备丢失了,与TestSensor2= Error reading file at /sys/bus/w1/devices//w1_slave
相同。
使用g++ -Wall -o oww ofen-warmwasser.cpp -lwiringPi
直接在rPi上编译代码
这是我的代码
#include <wiringPi.h> // library to access the GPIO Pins on a raspberryPI !!deprecated!!
#include <iostream> // cout
#include <fstream> // file stream access
#include <string> // string class
#include <sstream> // string stream needed for file access to put data into string
//... code skipped ...
class temperaturSensor {
private:
string device;
string baseDir = "/sys/bus/w1/devices/";
string tempFile = "/w1_slave";
string path = baseDir + device + tempFile;
stringstream buffer;
string data;
string strTemp;
double temp = 987.6;
public:
// constructor declaration
temperaturSensor(string str);
// methodes
double temperatur() {
//cout << "----->>>> " << device << " ----->>>> " << path << endl;
ifstream infile(path);
if (infile) {
buffer << infile.rdbuf();
data = buffer.str();
infile.close();
}
else {
infile.close();
cout << "Error reading file at " << path << endl;
return -100;
}
size_t crcCheck = data.find("YES");
if (crcCheck == string::npos) {
cout << "CRC fail not reading temperatur" << endl;
return -101;
}
size_t TempPos = data.find("t=");
if (TempPos == string::npos) {
cout << "failed to find value -> abort!" << endl;
return -102;
}
strTemp = data.substr(TempPos+2);
temp = stod(strTemp)/1000;
return temp;
}
};
// constructor
temperaturSensor::temperaturSensor(string str) {
device = str;
}
int main(void) {
// test setup
pump boilerpumpe(21);
valve boilervalve(28);
string tSensor1 = "28-3c01a81688f4";
temperaturSensor testSensor1(tSensor1);
temperaturSensor testSensor2("28-3c01a816d9c1");
while (true)
{
boilerpumpe.on();
boilervalve.close();
cout << "TestSensor1= " << testSensor1.temperatur() << "°C \nTestSensor2= " << testSensor2.temperatur() << "°C" << endl;
delay(5*1000);
boilerpumpe.off();
boilervalve.open();
delay(5*1000);
}
return 0;
}
您可能会在代码中看到,我尝试了不同的操作,从string device;
到设备变量的cout
从private到public:从类的结尾到开始:它正确显示,我认为问题出在string path = baseDir + device + tempFile;
,我也尝试过string path = baseDir.append(device);
,但没有成功
任何帮助,如果需要更多信息,请告诉我,我会尝试提供
感谢蚀刻
解决方法
在类声明中:
string device;
该类成员默认初始化为空字符串。
string baseDir = "/sys/bus/w1/devices/";
该类成员默认初始化为给定的字符串。
string tempFile = "/w1_slave";
该类成员默认初始化为给定的字符串。
string path = baseDir + device + tempFile;
该类成员被初始化为组合字符串。请记住,device
是一个空字符串,因此路径最终会被设置为“ / sys / bus / w1 / devices // w1_slave”,这就是您的代码最终试图打开并失败的原因。
在您的类的构造函数中:
device = str;
这将为device
设置一个新值。但是,为时已晚,因为另一个类成员(尤其是path
)已经初始化。设置device
的值后,将不会再次对其进行初始化。 C ++无法以这种方式工作。
最简单的解决方法是让构造函数使用其初始化部分按顺序初始化所有类成员。
,您有三个成员变量的默认成员初始化程序。这些操作将在构造函数主体中分配device
之前执行,此时设备仍将是一个空字符串。
class temperaturSensor {
private:
string device;
string baseDir = "/sys/bus/w1/devices/";
string tempFile = "/w1_slave";
string path = baseDir + device + tempFile;
baseDir
和tempFile
的值似乎从未改变,因此,将它们设为const静态,然后在初始化{{之后初始化path
1}},或者使用传递给它的参数值。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。