LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
LinkBack Search this Thread
Old 01-30-2013, 09:38 PM   #1
Dornith
Member
 
Registered: Jul 2012
Location: U.S.A
Distribution: Archlinux Multilib, Debian 7.0.0 32b
Posts: 149

Rep: Reputation: Disabled
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'
 
Old 01-31-2013, 02:47 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,460

Rep: Reputation: 445Reputation: 445Reputation: 445Reputation: 445Reputation: 445
At which line of your source you get the error message?
 
Old 01-31-2013, 03:49 AM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by Dornith View Post
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.
Old 01-31-2013, 04:09 PM   #4
Dornith
Member
 
Registered: Jul 2012
Location: U.S.A
Distribution: Archlinux Multilib, Debian 7.0.0 32b
Posts: 149

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by JohnGraham View Post
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.)
 
Old 01-31-2013, 05:35 PM   #5
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Centos
Posts: 4,969

Rep: Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075Reputation: 1075
Quote:
Originally Posted by JohnGraham View Post
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.
 
Old 02-01-2013, 03:21 AM   #6
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by Dornith View Post
(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()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Inheriting file permission abhinav4 Linux - Newbie 6 10-14-2012 04:58 PM
Problems with inheriting permissions kcommins Linux - Newbie 10 01-17-2010 06:33 PM
Inheriting file permissions Hardip Linux - Security 4 07-02-2006 11:56 PM
inheriting permissions steve007 Linux - Security 3 07-05-2005 11:09 PM
can't use protected inner class when inheriting the outer Dominik Programming 6 02-02-2004 01:12 AM


All times are GMT -5. The time now is 01:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration