我的子类的属性是惰性初始化的 我在C++和一个子类中创建了一个父类,它有两个属性:为了使用我的子类,我声明它,并将它的地址分配给父类的指针。因此,我使用父类的方法
我的问题是:当我使用cyclePulse方法时,没有参数int trigger、int echo和类_trigger和_echo中的属性,该方法无法正常工作。我想这是因为属性_trigger和_echo是惰性初始化的,或者是因为我在创建对象时没有使用new关键字我的子类的属性是惰性初始化的 我在C++和一个子类中创建了一个父类,它有两个属性:为了使用我的子类,我声明它,并将它的地址分配给父类的指针。因此,我使用父类的方法,c++,C++,我的问题是:当我使用cyclePulse方法时,没有参数int trigger、int echo和类_trigger和_echo中的属性,该方法无法正常工作。我想这是因为属性_trigger和_echo是惰性初始化的,或者是因为我在创建对象时没有使用new关键字 class ISensor { ... public: ISensor(); virtual ~ISensor(); ... virtual int connect() = 0; virtual
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;
}
这里给出我之前的评论作为可能的答案,因为我假设未初始化的变量导致了您的错误:
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;
}
HCSR04s构造函数?在不接受任何参数的构造函数中,您将_trigger和_echo设置为什么?我编辑了消息。很抱歉。我忘记了构造器。但是我正在初始化构造函数上的attr。什么意思是不能正常工作?它是如何正常工作的?我在代码中没有看到任何延迟初始化。但是您正在cyclePulse方法中使用check uninitialized。这可能导致提前致电bcm2835_st__read
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;
}