在类中调用另一个构造函数作为全局[关闭]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在类中调用另一个构造函数作为全局[关闭]相关的知识,希望对你有一定的参考价值。

我正在为我的DHT传感器创建一个库。我有两个名字为AM_2301.cppAM_2301.h的文件:

AM_2301.h

#ifndef AM2301_h
#define AM2301_h

#include "Arduino.h"

struct Two_float {
    float temp;
    float humidity;
};

extern int pinn;

class AM_2301
{
  public:
    AM_2301(int pin);
    void begin();
    struct Two_float read();
  private:
    int _pin;
};

#endif

AM_2301.cpp

#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"

//#define pinn 3
int pinn;
DHT dht(pinn, DHT21);

AM_2301::AM_2301(int pin)
{
    pinMode(pin, OUTPUT);
    Serial.print("pinn: ");
    Serial.println(pinn);
    _pin = pin;
    pinn = pin;
    //DHT dht(pinn, DHT21);
}


void AM_2301::begin(){
    dht.begin();
}

struct Two_float AM_2301::read()
{
    struct Two_float output;
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h))
    {
        Serial.println("Failed to read from DHT");
        t = 0;
        h = 0;
    }
    output = {t, h};
    return output;
}

main.cpp

#include "Arduino.h"
#include "AM_2301.h"

int pin =3 ;

AM_2301 AM(pin);

void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    AM.begin();
}

void loop() {

    struct Two_float val;
    val = AM.read();
    Serial.print("Temp: ");
    Serial.print(val.temp);
    Serial.print(" , ");
    Serial.print("Humidity: ");
    Serial.println(val.humidity);
    delay(2000);
    // put your main code here, to run repeatedly:
}

但问题是我想在构造函数中声明一个引脚号,并且该引脚转到AM_2301.cpp中的另一个构造函数但我不知道如何实现它。我想让dht对象成为我班级中所有其他函数的全局对象。

答案

你需要使DHT对象成为AM_2301类的一部分,然后使用member initializer list对其进行初始化。

AM_2301.h

#ifndef AM2301_h
#define AM2301_h

#include "Arduino.h"

struct Two_float {
    float temp;
    float humidity;
};

class AM_2301
{
  public:
    AM_2301(int pin);
    void begin();
    struct Two_float read();
  private:
    DHT dht; // add a DHT object
};

#endif

AM_2301.cpp

#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"

AM_2301::AM_2301(int pin) : dht(pin, DHT21) {} // initialize the DHT object

// other code stays the same
另一答案

最后我根据paddy的answer找到了一个解决方案,我必须使用来自我的静态实例的指针。这是修改后的AM_2301.cpp

#include "Arduino.h"
#include "AM_2301.h"
#include "DHT.h"

static DHT *dht = NULL;  //define the pointer


AM_2301::AM_2301(int pin)
{
    pinMode(pin, OUTPUT);
    Serial.print("_pin: ");
    Serial.println(_pin);
    _pin = pin;
    dht = new DHT(_pin, DHT21);  //pass the initialization
}


void AM_2301::begin(){
    dht->begin();
}

struct Two_float AM_2301::read()
{
    struct Two_float output;
    float h = dht->readHumidity();
    float t = dht->readTemperature();
    // check if returns are valid, if they are NaN (not a number) then something went wrong!
    if (isnan(t) || isnan(h))
    {
        Serial.println("Failed to read from DHT");
        t = 0;
        h = 0;
    }
    output = {t, h};
    return output;
}

以上是关于在类中调用另一个构造函数作为全局[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

用另一个函数(都在类中)调用函数时出现问题。没有错误代码 C++

python 类中的函数

Tkinter - 在类中使用Button命令从另一个类调用函数

如何在另一个类中使用带有构造函数的类?

C++学习 之 类中的特殊函数和this指针

python 在类中从一个函数中调用另一个函数中的变量