3

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?

Grace Note
  • 3,205
  • 4
  • 35
  • 55
Rich
  • 2,805
  • 8
  • 42
  • 53

6 Answers6

15

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
  • 733,204
  • 149
  • 1,241
  • 1,454
3

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
  • 411
  • 2
  • 6
  • 7
2

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
  • 88,451
  • 51
  • 221
  • 321
2

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
  • 10,492
  • 3
  • 28
  • 30
1

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
  • 176,835
  • 32
  • 241
  • 292
1

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
  • 4,627
  • 6
  • 37
  • 49
  • 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 Apr 10 '09 at 19:41