C++ 字符串不连接成 class

C++ string does not concatenate in a class

我尝试编写一个小程序来控制泵和阀门,这些泵和阀门与 raspberry pi 3B+ 上的两个 ds18b20 温度传感器相连。我为泵定义了一个 class,为阀门定义了一个,将来会添加更多的泵和阀门,我还为通过一根电线连接的 ds18b20 传感器创建了一个 class。传感器可以通过“文件”读出,因此有一个基本目录 string baseDir = "/sys/bus/w1/devices/";,后跟设备 ID string tSensor1 = "28-3c01a81688f4"; 和温度“文件”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 这是我的代码

#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; 变量声明为 private: 到 public: 从 class 的结尾到开头,当我 cout 设备变量正确显示时,我认为问题出在此处 string path = baseDir + device + tempFile;,我也尝试 string path = baseDir.append(device); 但没有成功

感谢任何帮助,如果需要更多信息,请告诉我什么,我会尽力提供

感谢蚀刻

在您的 class 声明中:

string device;

此 class 成员 default-initialized 为空字符串。

string baseDir = "/sys/bus/w1/devices/";

这个 class 成员得到 default-initialized 到给定的字符串。

string tempFile = "/w1_slave";

这个 class 成员得到 default-initialized 到给定的字符串。

string path = baseDir + device + tempFile;

并且这个 class 成员被初始化为组合字符串。请记住,device 是一个空字符串,因此路径最终设置为“/sys/bus/w1/devices//w1_slave”,这是您的代码最终尝试打开的内容,但失败了。

在你的 class 的构造函数中:

   device = str;

这为 device 设置了一个新值。但是为时已晚,因为另一个 class 成员,特别是 path 已经初始化。在设置 device 的值后,它不会再次初始化。 C++ 不是这样工作的。

此处最简单的修复方法是让您的构造函数使用其初始化部分按顺序初始化所有 class 成员。

您有三个成员变量的默认成员初始值设定项。这些将在构造函数主体中的 device 赋值之前执行,此时 device 仍将是空字符串。

class temperaturSensor {
  private:
    string device;
    string baseDir = "/sys/bus/w1/devices/";
    string tempFile = "/w1_slave";
    string path = baseDir + device + tempFile;

似乎 baseDirtempFile 的值永远不会改变,因此将它们设为常量静态可能是最简单的,然后在初始化后初始化 path device 在您的构造函数中,或使用传递给它的参数的值。