简体   繁体   English

虚函数的C ++问题

[英]C++ problem with virtual functions

I need to write something in C++. 我需要用C ++编写一些东西。 I have problem with virtual functions. 我对虚拟功能有疑问。

For example, in header file Human.h I have this: 例如,在头文件Human.h我具有以下内容:

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

In Human.cpp file I have this: Human.cpp文件中,我有:

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

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

I get these compile errors: 我得到这些编译错误:

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

You have forgotten to end the class definition with a ; 您已经忘记了以“;”结尾的类定义;

It should read 它应该读

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

This will likely make the error go away. 这可能会使错误消失。 Also, always read the compiler's output: 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 另外,请始终阅读编译器的输出: 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM