我需要用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++ - 虚函数的C++问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4724110/