-2

I want to use the virtual function in my program. However, VSCode keeps telling me errors in the redefinition of the function.

This is my error message:

exp8_2.cpp:29:7: error: redefinition of 'float Mammal::getweight() const'
 float Mammal::getweight() const
       ^~~~~~
exp8_2.cpp:25:23: note: 'virtual float Mammal::getweight() const' previously defined here
         virtual float getweight() const {};
                       ^~~~~~~~~

It seems like the virtual function could not be overrode

Below is my code:

#include <stdio.h>
#include <string.h>
#include <iostream>

using namespace std;

class Mammal
{
    protected:
        float weight;
        
    public:
        
        Mammal(float wei)
        {
            this->weight=wei;
        }

        
        Mammal(){};

        ~Mammal(){};

        virtual void Speak() const {};
        virtual float getweight() const {};

};

float Mammal::getweight() const
{
    return this->weight;
}

class Dog:public Mammal
{
    protected:
        float height;

    public:
        Dog(float wei,float hei):Mammal(wei)
        {
            this->height=hei;
        }
        
       
        Dog(){};

        ~Dog(){};

        void Speak()const {};
        float getweight () const{};
};

void Dog::Speak() const
{
    cout<<"Woof!"<<endl;
};

float Dog::getweight() const
{
    return this->weight;
};

int main()
{
    Mammal mal(500);
    Dog doggo(500,1.5);

    Mammal *point;
    point=&mal;
    point->getweight();
    point->Speak();

    point=&doggo;
    point->getweight();
    point->Speak();
}

I could provide more info if the problem is not on my code but VScode.

rioV8
  • 24,506
  • 3
  • 32
  • 49
  • 2
    You define the function inside the class, and again outside the class. That pair of braces `{}` is the body of the function (which happens to be empty). Drop them. – Igor Tandetnik May 22 '22 at 13:46
  • *"It seems like the virtual function could not be overrode"* -- I'll grant that this is a reasonable first hypothesis (i.e. a guess). However, it is also something you could have tested with a simple experiment -- remove the `virtual` keyword and see if the error goes away. Another relatively easy experiment would be suggested if you noticed that `Dog` does not appear in the error message (notice details!) -- remove the definition of `Dog` from your example. If you did both of these experiments, you would have gotten a better picture of where the issue lies, leading to a revised hypothesis. – JaMiT May 22 '22 at 14:03

1 Answers1

3
virtual float getweight() const {};

Here's your first definition of this class method. That's what the {} part means (and it doesn't need a trailing semicolon).

float Mammal::getweight() const
{
    return this->weight;
}

And here's the 2nd definition. Hence the compilation error. C++ has what's known as a "One Definition Rule". Something only gets to be defined exactly once, otherwise your C++ compiler will be very sad.

Just change the first definition to a declaration:

virtual float getweight() const;
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • C:\Users\lienh\AppData\Local\Temp\cc6Al5Eg.o:exp8_2.cpp:(.text$_ZN6MammalC2Ef[__ZN6MammalC2Ef]+0xa): undefined reference to `vtable for Mammal' C:\Users\lienh\AppData\Local\Temp\cc6Al5Eg.o:exp8_2.cpp:(.text$_ZN6MammalC1Ef[__ZN6MammalC1Ef]+0xa): undefined reference to `vtable for Mammal' C:\Users\lienh\AppData\Local\Temp\cc6Al5Eg.o:exp8_2.cpp:(.text$_ZN6MammalD2Ev[__ZN6MammalD2Ev]+0xa): undefined reference to `vtable for Mammal" – Tim heard you May 22 '22 at 14:11
  • However, this is the new errors I got. What's the problem here? – Tim heard you May 22 '22 at 14:12
  • See https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – Sam Varshavchik May 22 '22 at 14:18