LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   C++ inheriting a method from itself? (http://www.linuxquestions.org/questions/programming-9/c-inheriting-a-method-from-itself-4175447922/)

Dornith 01-30-2013 10:38 PM

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'

NevemTeve 01-31-2013 03:47 AM

At which line of your source you get the error message?

JohnGraham 01-31-2013 04:49 AM

Quote:

Originally Posted by Dornith (Post 4881049)
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?

Dornith 01-31-2013 05:09 PM

Quote:

Originally Posted by JohnGraham (Post 4881200)
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.)

johnsfine 01-31-2013 06:35 PM

Quote:

Originally Posted by JohnGraham (Post 4881200)
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.

JohnGraham 02-01-2013 04:21 AM

Quote:

Originally Posted by Dornith (Post 4881667)
(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()



All times are GMT -5. The time now is 03:28 PM.