C++为什么继承不起作用?

use*_*978 1 c++ inheritance

在Shape.hpp中:

class Shape {   
    public:
    char c;
    virtual void paint();
    ...
};
Run Code Online (Sandbox Code Playgroud)

在Triangle.hpp中:

#include "Shape.hpp"

class Triangle : public Shape {
    ...
};
Run Code Online (Sandbox Code Playgroud)

在Triangle.cpp中:

...

void Triangle::paint() {
    ...
}

...
Run Code Online (Sandbox Code Playgroud)

编译时:

error: class ‘Triangle’ does not have any field named ‘c’
error: no ‘void Triangle::paint()’ member function declared in class ‘Triangle’
Run Code Online (Sandbox Code Playgroud)

我不明白为什么Triangle没有其父类Shape的字段和成员函数.怎么解决这个问题?

jua*_*nza 6

您还需要声明paint()成员函数Triangle:

class Triangle : public Shape 
{
 public:
    void paint() override;
};
Run Code Online (Sandbox Code Playgroud)