Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-30-2013, 09:38 PM
|
#1
|
|
Member
Registered: Jul 2012
Location: U.S.A
Distribution: Slackware 14-beta Multilib, Ubuntu 12.04.1
Posts: 134
Rep: 
|
C++ inheriting a method from itself?
I have some C++ code with a virtual method in it, and for some reason it's telling me that I need to implement that virtual method within itself. Doesn't that beat the whole point of a virtual method though? Here's what I have:
Code:
class Creature
{
protected:
virtual void reset() = 0;
void dealDamage(Creature c)
{}
}
And Eclipse returns:
Code:
The type 'Creature' must implement the inherited pure virtual method 'Creature::reset'
|
|
|
|
01-31-2013, 02:47 AM
|
#2
|
|
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,039
|
At which line of your source you get the error message?
|
|
|
|
01-31-2013, 03:49 AM
|
#3
|
|
Member
Registered: Oct 2009
Posts: 451
Rep: 
|
Quote:
Originally Posted by Dornith
Code:
The type 'Creature' must implement the inherited pure virtual method 'Creature::reset'
|
It sounds like you're trying to instantiate an instance of this class, either by declaring one or with new.
Also, your function dealDamage() is taking an instance of Creature - it cannot do this, as Creature is an abstract type with virtual methods. Take a pointer (or possibly a reference) instead.
Is this the first (very very first) error you get?
Last edited by JohnGraham; 01-31-2013 at 03:51 AM.
|
|
|
1 members found this post helpful.
|
01-31-2013, 04:09 PM
|
#4
|
|
Member
Registered: Jul 2012
Location: U.S.A
Distribution: Slackware 14-beta Multilib, Ubuntu 12.04.1
Posts: 134
Original Poster
Rep: 
|
Quote:
Originally Posted by JohnGraham
It sounds like you're trying to instantiate an instance of this class, either by declaring one or with new.
Also, your function dealDamage() is taking an instance of Creature - it cannot do this, as Creature is an abstract type with virtual methods. Take a pointer (or possibly a reference) instead.
Is this the first (very very first) error you get?
|
I see it now. That makes a lot of sense. I'm usually a Java programmer but I see what you're saying.
Also, it's the only error I get. (Note, I'm not actually compiling it, this is just Eclipse telling me I have an error.)
|
|
|
|
01-31-2013, 05:35 PM
|
#5
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,674
|
Quote:
Originally Posted by JohnGraham
Also, your function dealDamage() is taking an instance of Creature
|
It might not be "also". That might be the entire problem.
An object of the class with a pure virtual function cannot exist, which also implies it cannot be passed by value.
As already explained, you can have a reference or a pointer to an object of that class, but at run time that will point to the base class portion of a derived object. It cannot point to a stand alone object.
Quote:
|
(Note, I'm not actually compiling it, this is just Eclipse telling me I have an error.)
|
It is a misleading error message, but basically correct. There is use of an object in which a virtual function is undefined. The compiler generally can't know which is the bug: Was it a bug to use an object of that class? Or was it a bug to have the function undefined? Whatever code analysis generated that error jumped to the conclusion that having the function undefined was the error. One can never be sure of that error. The error always might be using an object of a type for which only pointers and references can be used. In this case it is far more plausible that use of the object (passing it by value) was wrong, and leaving the function undefined was not wrong. But this unfortunately is not worse than average for error message quality in C++. Distinguishing correct from incorrect is hard enough. Reporting the correct aspect of something incorrect is almost impossible.
|
|
|
|
02-01-2013, 03:21 AM
|
#6
|
|
Member
Registered: Oct 2009
Posts: 451
Rep: 
|
Quote:
Originally Posted by Dornith
(Note, I'm not actually compiling it, this is just Eclipse telling me I have an error.
|
Ah, I was wondering why you didn't get the usual message. Next time, try compiling it and the compiler's error message may be more helpful (or at least give you another perspective on the problem). As it happens if I cut & paste your code and try to compile it with g++, the error message is pretty clear:
Code:
$ g++ test.cpp
test.cpp:5:6: error: cannot declare parameter ‘c’ to be of abstract type ‘Creature’
test.cpp:1:7: note: because the following virtual functions are pure within ‘Creature’:
test.cpp:4:14: note: virtual void Creature::reset()
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:25 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|