我的子类别的属性是延迟初始化的

时间:2018-10-02 10:15:23

标签: c++

我用C ++创建了一个父类,并创建了一个具有两个属性的子类:_trigger_echo。为了使用我的子类,我对其进行了声明,并将其地址分配给父类的指针。因此,我使用父类的方法。

我的问题:当我使用没有参数(cyclePulse)和类的属性(int trigger, int echo_trigger)的方法_echo时,该方法不起作用正确地。我猜这是因为_trigger_echo属性是延迟初始化的,或者是因为我在创建对象时没有使用new关键字。

class ISensor {
...
public:
    ISensor();
    virtual ~ISensor();
    ...
    virtual int connect() = 0;
    virtual char * readRequest() = 0;
    virtual int disconnect() = 0;
};

class HCSR04: public ISensor {
private:
    int _trigger;
    int _echo;
public:
    HCSR04();
    HCSR04(int trigger, int echo);
    ...
    int connect();
    char * readRequest();
    uint64_t cyclePulse(int trigger, int echo);
    float distanceCentimeters();
};

这是HCSR04.cpp的实现。 编辑:我忘记了构造函数。

HCSR04::HCSR04() {
    _echo = RPI_V2_GPIO_P1_13;
    _trigger = RPI_V2_GPIO_P1_15;
}
HCSR04::HCSR04(int trigger, int echo) {
    _echo = echo;
    _trigger = trigger;
}    
char * HCSR04::readRequest() {
    float preCent = distanceCentimeters();
    char* buf = new char[20];
    sprintf(buf, "%.10f", preCent);
    return buf;
}
float HCSR04::distanceCentimeters() {
    return (float) cyclePulse(_trigger, _echo) / 55.5;
}
uint64_t HCSR04::cyclePulse(int trigger, int echo) {
    uint64_t width, begin, start, end;
    int max = 80, check;

    begin = bcm2835_st_read();

    // Emit pulse for 10 microseconds
    bcm2835_gpio_write(_trigger, HIGH); // Set trigger state HIGH
    bcm2835_delayMicroseconds(10);  // Wait 10 microseconds
    bcm2835_gpio_write(_trigger, LOW);  // Set trigger state LOW

    while (bcm2835_gpio_lev(_echo) == LOW && check < max) {
        start = bcm2835_st_read();
        check = (int) begin - start;
    }
    while (bcm2835_gpio_lev(_echo) == HIGH) {
        bcm2835_delayMicroseconds(1);
    }
    end = bcm2835_st_read();

    width = end - start;

    return width;
}

我正在使用这样的课程:

HCSR04 deviceUltrasonic;
ISensor * sensorUltrasonic = &deviceUltrasonic;
char* readRequestArray = sensorUltrasonic->readRequest();

1 个答案:

答案 0 :(得分:1)

在这里给出我以前的评论作为可能的答案,因为我认为未初始化的变量正在引起您的错误:

uint64_t HCSR04::cyclePulse(int trigger, int echo) {
    uint64_t width, begin, start, end;
    int max = 80, check = 0;  // <<< init check to 0. 
                              // Btw: size_t or any other unsigned type matches
                              // better the purpose of what you want to achieve.

    begin = bcm2835_st_read();

    // begin is not used afterwards. Did you mean to initialize "start" here?
    start = begin;

    // Emit pulse for 10 microseconds
    bcm2835_gpio_write(_trigger, HIGH); // Set trigger state HIGH
    bcm2835_delayMicroseconds(10);  // Wait 10 microseconds
    bcm2835_gpio_write(_trigger, LOW);  // Set trigger state LOW

    while (bcm2835_gpio_lev(_echo) == LOW && check < max) {
        start = bcm2835_st_read();
        check = (int) begin - start;
    }
    while (bcm2835_gpio_lev(_echo) == HIGH) {
        bcm2835_delayMicroseconds(1);
    }
    end = bcm2835_st_read();

    width = end - start;

    return width;
}