虚函数的C++问题

1 c++ virtual function

我需要用C++编写一些东西.我有虚拟功能的问题.

例如,在头文件中Human.h我有这个:

class Human
{
    public:
        virtual int Age();
        Human();
        ~Human();
}
Run Code Online (Sandbox Code Playgroud)

Human.cpp文件中我有这个:

#include<iostream>
#include "Human.h"

int Human::Age()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到这些编译错误:

Error    4    error C2371: 'Human::Age' : redefinition; different basic types    c:\users\jan\desktop\testc\testc\human.cpp    5    1    TestC
Error    3    error C2556: 'Human Human::Age(void)' : overloaded function differs only by return type from 'int Human::Age(void)'    c:\users\jan\desktop\testc\testc\human.cpp    5    1    TestC
Error    2    error C2628: 'Human' followed by 'int' is illegal (did you forget a ';'?)    c:\users\jan\desktop\testc\testc\human.cpp    4    1    TestC
Run Code Online (Sandbox Code Playgroud)

Ren*_*ger 16

你忘了用a结束类定义 ;

它应该读

class Human
{
public:
    virtual int Age();
    Human();
    ~Human();
};
Run Code Online (Sandbox Code Playgroud)

这可能会使错误消失.另外,始终阅读编译器的输出:Error 2 error C2628: 'Human' followed by 'int' is illegal (did you forget a ';'?) c:\users\jan\desktop\testc\testc\human.cpp 4 1 TestC