Jump to content
  • Advertisement
Sign in to follow this  
Quiggy

Virtual functions in seperate files?

This topic is 4861 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

I am trying to get up a virtual destructor in a seperate file from where it has been declared. I have this in my Players.h file. virtual ~Player(); So naturally in my Player.cpp I want to have my destructor set up so... virtual Player::~Player() { } However I keep on getting errors (eg. virtual class outside declaration) when I do this. Works fine if I remove the 'Virtual' but this is not what I want. I have tried putting the virtual keyword before player, after player, after the two colons, but no luck. It just never compiles if I put the virtual keyword in the CPP file. Note that this happens for all my virtual functions, not just the destructor. I know that the virutal keyword is not really needed, but I like to see it in the code so I know that it is indeed virtual. Any ideas what I'm doing wrong. I am using Dev-C++ 4.9.9.2. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
You only need to (and should) put the virtual keyword in the declaration of the function. It will be automatically be inferred from there that the function is virtual, and will give errors if you insist to put it.

Just don't put it.

Share this post


Link to post
Share on other sites
I believe using the "virtual" keyword in the .cpp file is more than just unnecessary, it's not allowed. You can only use it inside the class definition.

Share this post


Link to post
Share on other sites
Hmm, Interesting.

I looked back at the book I'm reading right now and re-read that section more closely. I won't go into the details, but your right. I misunderstood a certain section in there...

Thanks for the help!

Share this post


Link to post
Share on other sites
if you want to remind yourself that a function is virtual put some comments above it sayings so ie

in .h file

class foo
{
virtual ~foo()
};


in .cpp file


/*****************************
vitual ~foo()
destroys object

******************************/
foo::~foo()
{
...
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!