0

Trackオブジェクトを含む multimap メンバーを持つクラスがありNoteます。Noteクラスのメソッドの1つはこれです:

float Note::getValue(){
    float sample = generator->getSample(this); // not working
    return sample;
}

Noteにも type のメンバーがあり、そのクラスのメソッドGeneratorを呼び出す必要があります。現在のオブジェクトを渡す必要があり、キーワードを使用してそうしようとしましたが、それが機能せず、エラーが発生しました。getSampleNoteNotethisNon-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 に答える