我有一个名为 Generator 的界面,如下所示:
class Generator{
public:
virtual float getSample(Note ¬e)=0;
};
我的 Synth
类正在实现它,如下所示:
class Synth : public Generator{
public:
virtual float getSample(Note ¬e);
};
float Synth::getSample(Note ¬e){
return 0.5;
}
我正在尝试从我的 Note 类(它有一个生成器成员)调用 getSample 方法
class Note : public Playable{
public:
Generator *generator;
virtual float getValue();
};
float Note::getValue(){
float sample = generator->getSample(*this); // gets stuck here
return sample;
}
当我尝试运行时,它卡在上面代码中标记的行上。问题是我没有收到非常明确的错误消息。这是它停止后我能看到的:
最佳答案
您似乎从未初始化成员 Note::generator
,所以在其上调用函数是未定义的行为。
尝试,作为测试:
float Note::getValue(){
generator = new Synth;
float sample = generator->getSample(*this); // gets stuck here
return sample;
}
如果可行,请返回并检查您的逻辑。使用 std::unique_ptr<Generator>
而不是原始指针。为 Node
创建构造函数.在那里初始化指针。
关于c++ - 继承实现不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13198526/