【发布时间】:2013-05-15 09:56:42
【问题描述】:
所以我有一个名为Person
的类class Person{
private:
public:
Person();
}
还有 1 个名为 Patient
的班级class Patient : public Person{
private:
string text_;
public:
Patient();
void setSomething(string text){ text_ = text; }
}
现在我创建了一个包含 5 个人的数组
Person *ppl[5];
并在数组的每个键中添加 5 个患者,如
ppl[0] = new Patient();
ppl[1] = new Patient();
ppl[2] = new Patient();
ppl[3] = new Patient();
ppl[4] = new Patient();
现在我想像这样从 Patient 类中调用 setSomething 函数
ppl[0]->setSomething("test text");
但我不断收到以下错误:
class Person has no member named setSomething
【问题讨论】:
-
问题是什么?编译器非常清楚出了什么问题。
Person没有setSomething方法,因此您不能在Person或指向Person的指针上调用它。 -
我添加了 Patient 扩展了 Person 所以我认为它可以工作......
-
既然数组的所有元素都是新的 Patient 类,我可以调用 Patient 类的函数吗?
-
@juanchopanza 你是对的,对不起!
-
@fxuser:数组的元素是患者,但就编译器而言,保证它们是人员,仅此而已:
Person *ppl[5]。
标签: c++ class oop function derived