0

I have a Track class with a multimap member that contains Note objects. One of the methods of the Note class is this:

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

Note also has a member of Generator type and I need to call the getSample method of that class, which expects a Note as argument. I need to pass the current Note object and tried doing so with the keyword this but that's not working and giving me the error Non-const lvalue reference to type 'Note' cannot bind to a temporary of type 'Note *'.

This is what the method definition for getSample looks like:

virtual float getSample(Note &note);

As you can see I'm using a reference because this method is called very very often and I can't afford to copy the object. So my question is: any ideas how I can get this done? Or maybe change my model to something that could work?

EDIT

I forgot to mention that I also had tried using generator->getSample(*this); but this wasn't working either. I'm getting this error message:

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)

This is what my Generator class looks like (the getSample method is implemented in a subclass):

class Generator{
public:
    virtual float getSample(Note &note);

};

4 Answers4

3

this is a pointer, your code takes a reference. Try this

float sample = generator->getSample(*this);
john
  • 7,897
  • 29
  • 27
  • 1
    Well that error means what it says, you haven't defined one of the virtual methods you've declared. Either that or you are building your code incorrectly. In any case `*this` is correct, the other problem is another problem. – john Nov 02 '12 at 13:58
  • 1
    Just noticed Denis Ermolin's answer. He's probably right, you need a pure virtual method. 'getSample method is implemented in a subclass' is not good enough. – john Nov 02 '12 at 14:01
2

this is a pointer in C++, so you'll need

float sample = generator->getSample(*this);
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • @Sled that's a completely different issue. Why didn't you post the error message to start with? Why didn't you post real code? Anyway - http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix deals with these errors. – Luchian Grigore Nov 02 '12 at 13:49
  • I thought using `getSample(*this);` was just as wrong (because it wasn't working either) so I only the first way I was getting errors. What do you mean with real code? –  Nov 02 '12 at 13:51
  • @Sled it means that you said you tried using `*this`, but in the question you use `this`. – Luchian Grigore Nov 02 '12 at 13:53
  • Yea, I had tried both ways and neither were working so I only posted my attempt without the `*` (because I thought both were wrong since they both didn't work). Sorry for the confusion –  Nov 02 '12 at 13:55
1

Pass reference, not a pointer to getSample(). That is write this way:

float Note::getValue(){
    float sample = generator->getSample(*this);
    return sample;
}
Yanushko
  • 21
  • 2
0

You must declare your Generator class as abstract, try this declaration:

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

But if you dont need it you must implement virtual function in base class anyway:

virtual float getSample(Note &note){}
Denis Ermolin
  • 5,530
  • 6
  • 27
  • 44