小编Luk*_*uke的帖子

我可以在不使用 include 的情况下扩展头文件中的类吗?

我有这三个文件:

Person在头文件中定义的抽象类:

人.h

class Person {
    public:
    char * name;
    virtual char * days_work(int count, int price) = 0;
};
Run Code Online (Sandbox Code Playgroud)

在头文件中定义的Miner扩展类Person

矿工.h

class Miner: public Person {
    public:
    int gold_mined;
    void days_work();
};
Run Code Online (Sandbox Code Playgroud)

以及Miner代码文件中成员函数的定义:

矿工.cpp

#include "Miner.h"
#include "Person.h"

Miner::Miner(char * name) {
    this->name = name;
    this->gold_mined = 0;
}
Miner::~Miner() {}
void Miner::days_work() {
    this->gold_mined += 10;
}
Run Code Online (Sandbox Code Playgroud)

当我编译这个(使用 gcc)时,我收到一个invalid use of incomplete type ‘struct Person’错误。我可以得到它通过将编译#include "Person.h"在顶部Miner.h …

c++ polymorphism class

2
推荐指数
1
解决办法
1805
查看次数

标签 统计

c++ ×1

class ×1

polymorphism ×1