C ++ проблема с виртуальными функциями - PullRequest
1 голос
/ 18 января 2011

Мне нужно что-то написать на 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

1 Ответ

16 голосов
/ 18 января 2011

Вы забыли завершить определение класса: ;

Оно должно читаться как

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...