我试图在头文件中定义派生类,并在cpp文件中定义它,但我有错误
我的项目文件
的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 from the 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
运行代码给我一个错误[错误]不是&#39; void AmericanPerson :: talkto(const Human&amp;)const&#39;在班级成员函数&#39; AmericanPerson&#39;
中声明它也可能是失败
感谢您的时间
答案 0 :(得分:4)
您需要像任何其他类成员函数一样声明虚拟覆盖:
class AmericanPerson : public Human
{
public:
AmericanPerson(const string & name);
void talkTo(const Human & person) const override;
};