c++ - how define derived class in header file and definition in a cpp file with method virtual -


i trying define derived class in header file , definition of in cpp file have errors

my project files

main.cpp

#include <iostream> #include "americanperson.h"  int main(int argc, char** argv)  {     americanperson joe("joe");     return 0; } 

human.h

#ifndef human_h__ #define human_h__  #include "definiciones.h" #include <string>  using namespace std;  class human {     protected:         string namec;     public:         human(const string & name);          virtual void talkto(const human & person) const;         string name() const; };  #endif 

human.cpp

#include "human.h"  human::human(const string & name) : namec(name){}  string human::name() const {     return namec; } 

americanperson.h

#ifndef americanperson_h__ #define americanperson_h__  #include "human.h" #include <string>  using namespace std;  class americanperson : public human {     public:         americanperson(const string & name); };  #endif 

americanperson.cpp

#include <iostream> #include "americanperson.h"   americanperson::americanperson(const string & name) : human(name) { }  //virtual method inherited base class human  void americanperson::talkto(const human & person) const //override ?  {     cout<< namec << "dice: hello " << person.name() << endl; } 

definiciones.h

#ifndef definiciones_h__ #define definiciones_h__  class human; class americanperson; class mexicanperson;  #endif 

to run code gives me error [error] not 'void americanperson :: talkto (const human &) const' declared in class member function 'americanperson'

it can failures

thanks time

you need declare virtual override other class member function:

class americanperson : public human {     public:         americanperson(const string & name);         void talkto(const human & person) const override; }; 

Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -