Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kylotan

Pure Virtual problem

This topic is 6934 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, me again. I''m more questions than answers this weekend, which makes a change I get the following errors when I compile: C:\CODING\TURNBASED\appinit.cpp(47) : error C2259: ''DXSoundManager'' : cannot instantiate abstract class due to following members: C:\CODING\TURNBASED\appinit.cpp(47) : warning C4259: ''bool SoundManager:laySound(enum SoundNumber,int,int)'' : pure virtual function was not defined C:\CODING\TURNBASED\appinit.cpp(47) : error C2259: ''DXSoundManager'' : cannot instantiate abstract class due to following members: C:\CODING\TURNBASED\appinit.cpp(47) : warning C4259: ''bool SoundManager:laySound(enum SoundNumber,int,int)'' : pure virtual function was not defined Line 47 is: theSoundManager = new DXSoundManager(); Now, I understand what these errors are and what they mean. What I don''t get is (a) Why I am getting the error - DXSoundManager (a derived class of SoundManager) certainly does have the named function, both in the class definition and in the accompanying CPP file. (b) Why I would get the error twice! And in one line. All my headers have inclusion guards. I just wondered if anyone had seen this before and had any idea what I was doing wrong. I have too much code to post it all, and I was hoping this was one of those weird situations with a certain way to fix it.

Share this post


Link to post
Share on other sites
Advertisement
It''s a bit hard to tell what''s going on without knowing what your DXSoundManager and SoundManager look like... So, if you could post (pieces of) the interface of those classes, we could tell some more.

Erik

Share this post


Link to post
Share on other sites
I have gotten this before, but it was because my base class still had an undefed virtual in it (like your OverlaySound fn [or whatever it''s called, i can''t read it because it shows up as some sort of face on this msg board )

If you''re positive you''ve defined it, then check for stupid errors, ex. did you define it as
BOOL OverlaySound(blah) {
}
or BOOL DxSoundManager::OverlaySound(blah) {
}

and was it defined in the class fn as a proper member? [ex. w/o the virtual keyword, etc).

I know this sounds trivial and stuff, but hey, it''s happened to me before

or maybe even try
theSoundManager = new DxSoundManager; //ie. w/o ()s

Share this post


Link to post
Share on other sites
I''ve run into this a lot lately, but it''s just an incorrect declaration or definition somewhere. Even a slight difference, such as constness, will cause that error. Try copying and pasting the function declaration from the base class into the derived class, so it matches exactly (removing the " = 0"). Then copy and paste that into the .cpp file as well for the definition, making the changes necessary to make it a definition.



- null_pointer
Sabre Multimedia

Share this post


Link to post
Share on other sites
Ok, here''s some copy and pasting. Please sign the NDA before reading further

In soundmanager.h:

class SoundManager
{
public:
// Interface for derived classes to implement
SoundManager();
virtual ~SoundManager();

// Sets it up
virtual bool Init() = 0;
// Knocks it down
virtual bool Shutdown() = 0;

// Plays a sound
virtual bool PlaySound(SoundNumber num, int volume, int pan) = 0;

// Member functions
bool IsSetup() const;

protected:
// SOME IRRELEVANT STUFF HERE
};

in dxsoundmanager.h:

class DXSoundManager : public SoundManager
{
public:
// Inherited interface ////////////////////
~DXSoundManager();
bool Init();
bool Shutdown();

bool PlaySound(SoundNumber num, int volume, int pan);


private:
// DX specific members
};

In dxsoundmanager.cpp

bool DXSoundManager:: PlaySound(SoundNumber num, int volume, int pan)
{
return false; // just a stub function for now
}


I am probably just overlooking something stupid as I''ve been staring at this code (and my annoying binary streams problem) for hours now...

Share this post


Link to post
Share on other sites
Did you actually create an implementation of the virtual function? I believe you have to do something like

class A: {
public: F() = 0;
}

A::F()
{
}

so actually have an (empty) implementation.

Share this post


Link to post
Share on other sites
quote:
Original post by axelp

Did you actually create an implementation of the virtual function?


I don''t believe an abstract base class needs to have its pure virtual functions to have a body. In fact, 99% of the time I don''t think you''d want it to have one. Am I right?

Share this post


Link to post
Share on other sites
A class needs only 1 pure virtual function to be abstract, and I''ve never seen a body for a pure virtual member (though I''ve never thought about it).

As for the error, the only thing I''d check is to make sure your SoundNumber is referencing the same typedef. I assume it is, but I don''t see anything else that would cause a problem. (Or try to replace it with an int, and see if maybe the compiler has a problem with the enum in this case)

Rock

Share this post


Link to post
Share on other sites
Well the obvious error from the code you''ve posted is that you STILL haven''t provided a body for the two pure virtual functions in the abstract base class.

Two things:
1. The implementations of the pure virtual functions should be normal virtual functions most of the time.

2. You NEED to provide a body for every pure virtual function in your class derived from the abstract base class, unless you want to use your derived class as another abstract base class.


#pragma DWIM // Do What I Mean!
~ Mad Keith ~
**I use Software Mode**

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!