【发布时间】:2012-11-02 13:13:49
【问题描述】:
我有一个 Track 类,它的 multimap 成员包含 Note 对象。 Note 类的方法之一是这样的:
float Note::getValue(){
float sample = generator->getSample(this); // not working
return sample;
}
Note 也有一个Generator 类型的成员,我需要调用该类的getSample 方法,该方法需要Note 作为参数。我需要传递当前的Note 对象并尝试使用关键字this 这样做,但这不起作用并给我错误Non-const lvalue reference to type 'Note' cannot bind to a temporary of type 'Note *'。
getSample 的方法定义如下所示:
virtual float getSample(Note ¬e);
如您所见,我使用的是引用,因为此方法被非常频繁地调用,而我无法复制对象。所以我的问题是:有什么想法可以完成这项工作吗?或者也许将我的模型更改为可行的模型?
编辑
我忘了提到我也尝试过使用generator->getSample(*this);,但这也不起作用。我收到此错误消息:
Undefined symbols for architecture i386:
"typeinfo for Generator", referenced from:
typeinfo for Synth in Synth.o
"vtable for Generator", referenced from:
Generator::Generator(Generator const&) in InstrumentGridViewController.o
Generator::Generator() in Synth.o
Generator::Generator(Generator const&) in InstrumentGridViewController.o
Generator::Generator() in Synth.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这就是我的Generator 类的样子(getSample 方法在子类中实现):
class Generator{
public:
virtual float getSample(Note ¬e);
};
【问题讨论】:
标签: c++ reference pass-by-reference