views:

828

answers:

6

Hi, I'm writing some code where I defined the following base class.

class Chorus{
  public:

    //Destructor
    virtual ~Chorus();

    //callback function
    virtual int callback( void *outputBuffer, void *notUsed, unsigned int 
       nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData );

    virtual void initializeDelayBuffer(void);

    virtual void destroyDelayBuffer(void);
};

I want to use this as a base class and not actually do anything with it on its own. So I have two seperate classes which are derived from this class Chorus. I wanted to do this to simply provide some basic constraints as to what any derived Chorus class MUST have to be considered usable in my program.

When I build my project (Visual Studio 2008), I get unresolved external symbol errors on all the virtual functions from this Chorus class. I'm guessing it's the typical error where I didn't make forward declarations of these functions. But, since they are virtual and I don't want them to actually be defined to do anything until they are defined within the deriving classes, what do I do to resolve this issue?

+14  A: 

If your intent is for them to be simply place holders for child classes to implement, then make them pure virtual functions by ending with = 0. For example

virtual void destroyDelayBuffer(void) = 0;

This makes the method "abstract" so to speak. The C++ compiler will not look for an actual definition of the method but instead will force all derived classes to implement these methods before an instance can be created.

JaredPar
So close! :) Nice answer.
Pablo Santa Cruz
+1  A: 

Use a pure virtual function:

  virtual int callback( void *outputBuffer, void *notUsed, unsigned int 
       nBufferFrames, double streamTime, RtAudioStreamStatus status, void *userData ) = 0;
Pablo Santa Cruz
+3  A: 

You need to declare those function as pure virtual.

virtual void initializeDelayBuffer(void) = 0;

That will create an interface of sorts, which is what you want.

MattJ
Agree, but please remove the void from the argument list; that's C not C++.
Brian Neal
+1  A: 

This is called a pure virtual function. In C++ you write a "=0" after the function name, but you probably want to read the FAQ on these.

http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.4

Uri
+2  A: 

You need to define the functions as pure virtual functions. To do this, add a " = 0" after each declaration. For example:

virtual void destroyDelayBuffer(void) = 0;
Jon Benedicto
+1  A: 

Just to add to the above answers, you cannot instantiate an object of a class containing pure virtual functions, so in the future if you intend to have objects of your base class, do remember to make the function non-pure virtual.

Shree
Yea, it's a base class to only be derived from. There will never be objects of it because it is too skeletal to do anything useful.
Rich