我是c ++的新手。
看起来成员变量_scheduleSize
得到的价值类型为" integer_half_max"没有任何理由。有人可以解释一下,为什么会发生这种情况?
调用
leds.addSchedulePoint(new ScheduledLedPoint(ScheduleTime(9), 0));
方法:
void ScheduledLeds::addSchedulePoint(ScheduledLedPoint *schedulePoint) {
Serial.print("_scheduleSize:");
Serial.println(_scheduleSize);
_schedule[_scheduleSize++] = schedulePoint;
Serial.print("_scheduleSize:");
Serial.println(_scheduleSize);
for (size_t i = 0; i < _scheduleSize; i++) {
Serial.println(_schedule[i]->getLevel());
}
}
导致这样的控制台输出:
_scheduleSize:0
_scheduleSize:1073680860
0
Exception (28):
epc1=0x40206514 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000010 depc=0x00000000
下面你可以看到整个班级:
class ScheduledLeds {
public:
ScheduledLeds(int pin);
void addSchedulePoint(ScheduledLedPoint *schedulePoint);
void process(ScheduleTime currentTime);
void freezeLedLevel(int targetLevel, int minutes);
int _pin;
private:
ScheduledLedPoint *_schedule[];
int _scheduleSize;
int _count;
int _size;
long _unfreezeTime;
int _lastLevel;
ScheduleTime _lastTime;
void setTransitionalLightLevel(ScheduleTime now, ScheduledLedPoint nextPoint);
void setLightLevel(int targetLevel);
};
ScheduledLeds::ScheduledLeds(int pin) {
pinMode(pin, OUTPUT);
_pin = pin;
_scheduleSize = 0;
_size = 10;
_unfreezeTime = millis();
ScheduledLedPoint *_schedule[_size];
}
void ScheduledLeds::addSchedulePoint(ScheduledLedPoint *schedulePoint) {
Serial.print("_scheduleSize:");
Serial.println(_scheduleSize);
_schedule[_scheduleSize++] = schedulePoint;
Serial.print("_scheduleSize:");
Serial.println(_scheduleSize);
for (size_t i = 0; i < _scheduleSize; i++) {
Serial.println(_schedule[i]->getLevel());
}
}
答案 0 :(得分:1)
ScheduledLedPoint *_schedule[];
不是有效的C ++,不应该编译。有些编译器接受它作为扩展 - 但即使在那里,它也是一个零大小的数组; _schedule[x]
展示了x
的任何值的未定义行为。
当您在构造函数中编写ScheduledLedPoint *_schedule[_size]
时,这似乎不会影响名为_schedule
的数据成员。相反,它会创建并立即销毁一个恰好名为_schedule
的局部变量;这实际上没有净效应。
对于可变大小的类数组数据结构,请使用std::vector
。
答案 1 :(得分:-1)
因为你有语法错误,包括这个递归函数