0

我有一个Track包含Note对象的 multimap 成员的类。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 &note);

正如你所看到的,我使用了一个引用,因为这个方法被非常频繁地调用并且我不能复制对象。所以我的问题是:有什么想法可以完成这项工作吗?或者也许将我的模型更改为可以工作的东西?

编辑

我忘了提到我也尝试过使用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 &note);

};
4

4 回答 4

3

this是一个指针,你的代码需要一个引用。试试这个

float sample = generator->getSample(*this);
于 2012-11-02T13:15:36.693 回答
2

this是 C++ 中的一个指针,所以你需要

float sample = generator->getSample(*this);
于 2012-11-02T13:15:34.747 回答
1

传递引用,而不是指向 getSample() 的指针。也就是这样写:

float Note::getValue(){
    float sample = generator->getSample(*this);
    return sample;
}
于 2012-11-02T13:18:27.673 回答
0

你必须将你的Generator类声明为抽象类,试试这个声明:

virtual float getSample(Note &note)=0; 
//this will force all derived classes to implement it

但是如果你不需要它,你必须在基类中实现虚函数:

virtual float getSample(Note &note){}
于 2012-11-02T13:59:43.337 回答