error: no ‘’ member function declared in class ''

12,512

Solution 1

In Mother's declaration you have:

virtual std::string getName() = 0; 

This is not just a virtual, but a pure virtual. The distinction between a virtual and a pure virtual is that the pure variety must have an override implemented in a derived class, even if you have provided an implementation in the base class. For example:

class Foo
{
public:
  virtual void DoIt() = 0 {};  // pure virtual.  Must be overridden in the derived class even though there is an implementation here
};

class Bar : public Foo
{
public:
  void DoIt(); // override of base
};

void Bar::DoIt()
{
  // implementation of override
}

You can't instantiate a class with un-implemented pure virtual methods. If you try, you will get a compiler error:

int main()
{
  Foo f;  // ERROR
  Bar b;  // OK
}

And that is exactly what you tried to do. You declared getName() to be pure virtual in Mother, but you did not override it in Child. Then you tried to instantiate a Child

int main()
{
   Child l("lol");

Which resulted in the compiler error.

To fix it, provide an override of getName() in the Child class.

Solution 2

You class child should override getName() method as it is pure virtual defined in class mother

Seems typo to me.. as std::string Mother::getName() is defined in child.cpp..

std::string Child::getName()
{
  return this->_name;
}
Share:
12,512
trolologuy
Author by

trolologuy

Basic skills in : C, C++, Html, CSS, Java, Python, Javascript, MYSQL, Linux Shell, Server administration... Working with Docker, Ansible and Python at the moment.

Updated on June 05, 2022

Comments

  • trolologuy
    trolologuy almost 2 years

    I'm trying to create a class containing a virtual function, which i would like to inherit in two child classes.

    I know some people already asked this (here and there for example), but i couldn't understand the answers.

    So i made a simplified example code of what i'm trying :

    //Mother .h file 
    
    #ifndef _MOTHER_H_
    #define _MOTHER_H_
    #include <iostream>
    
    class Mother
    {
     protected :
       std::string _name;
     public:
       Mother(std::string name);
       ~Mother();
       virtual std::string getName() = 0; 
    };
    
    #endif
    
    //Mother .cpp file
    
    #include "Mother.h"
    
    Mother::Mother(std::string name)
    {
      this->_name = name; 
    }
    
    Mother::~Mother()
    {
    }
    
    
    //Child.h file
    
    #ifndef _CHILD_H_
    #define _CHILD_H_
    #include "Mother.h"
    
    class Child : public Mother
    {
     private : 
       std::string _name;
     public:
       Child(std::string name);
       ~Child();
    };
    
    #endif
    
    
    //Child .cpp file
    
    #include "Mother.h"
    #include "Child.h"
    
    Child::Child(std::string name) : Mother(name)
    {
      this->_name = name;
    }
    
    Child::~Child()
    {
    }
    
    std::string Mother::getName()
    {
      return this->_name;
    }
    

    Here is my main.cpp file :

    //Main.cpp file
    #include "Child.h"
    
    int main()
    {
       Child l("lol");
    
       std::cout << l.getName() << std::endl;
    
       Mother& f = l;
    
       std::cout << f.getName() << std::endl;
    
       return 0;
     }
    

    Here's what the compilator says : (compiling with g++ *.cpp -W -Wall -Wextra -Werror)

    main.cpp: In function ‘int main()’:
    main.cpp:5:9: error: cannot declare variable ‘l’ to be of abstract type‘Child’
    In file included from main.cpp:1:0:
    Child.h:8:7: note:   because the following virtual functions are pure within ‘Child’:
    In file included from Child.h:6:0,
                 from main.cpp:1:
    Mother.h:14:23: note:   virtual std::string Mother::getName()
    

    What am i doing wrong ?

    (sorry if i made some english mistakes, i am not a native speaker).

  • trolologuy
    trolologuy over 10 years
    Well... this worked... but what's the point of redefining the Function in the Child if i already defined it in the Mother class?
  • Digital_Reality
    Digital_Reality over 10 years
    Infact that is wrong.. there should be no function name getName in class mother... as it is pure virtual (body =0). In this example, there must be only one getName ..and that must be in Child!
  • Joker_vD
    Joker_vD over 10 years
    @Digital_Reality Well, having definition for pure virtual function in the class where it was declared is not against the standard. It would allow you to implement Child::getName as return Mother::getName().
  • Joker_vD
    Joker_vD over 10 years
    std::string Child::getName() { return Mother::getName(); }?
  • trolologuy
    trolologuy over 10 years
    Thanks a lot ! Now i understood my mistake ! :) I still don't understand the logic behind it, but now i now how to use it.
  • Digital_Reality
    Digital_Reality over 10 years
    @Joker_vD yes but I am not really sure, if defining a pure virtual function does makes any sense!!
  • John Dibling
    John Dibling over 10 years
    @Joker_vD: that won't compile. There's no implementation in 'Mother'
  • Joker_vD
    Joker_vD over 10 years
    @JohnDibling There is, scroll down the code in the question all the way down.
  • John Dibling
    John Dibling over 10 years
    @Joker_vD: Ah, yes, I had missed that.