C++虚函数问题

标签 c++ function virtual

我需要用 C++ 编写一些东西。我对虚函数有疑问。

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

class Human
{
    public:
        virtual int Age();
        Human();
        ~Human();
}

Human.cpp 文件中我有这个:

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

int Human::Age()
{
    return 0;
}

我得到这些编译错误:

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

最佳答案

您忘记以 ; 结束类定义

应该是

class Human
{
public:
    virtual int Age();
    Human();
    ~Human();
};

这可能会使错误消失。另外,请始终阅读编译器的输出: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

关于C++虚函数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4724110/

相关文章:

excel - 具有精确 OR 值的嵌套 IF 语句

c++ - 跨 Symbian DLL 调用虚函数的问题

c++ - 有什么办法可以知道C++的派生类是否实现了非纯虚函数?

function - 之间的区别。和 : in Lua

c++ - 如果存在打开的文件 fd,inotify 不会引发 DELETE_SELF

c++ - 长除法余数的模式匹配

c++ - 函数模板的非依赖默认模板参数是否允许 SFINAE?

c - 使用预处理器指令在 C 中定义泛型函数

c++ - 使 operator<< 成为虚拟的?

c++ - "Multi-byte Character Set"当前的现代术语是什么