gpt4 book ai didi

c++ - 纯虚函数和 "cannot allocate an object"

转载 作者:行者123 更新时间:2023-11-28 00:29:26 26 4
gpt4 key购买 nike

<分区>

我有一个包含多个方法的抽象基类。

base.h

class Base
{
..
..

virtual void example_1() = 0;
virtual void example_2() = 0;

};

example.h
class Example : public Base
{
virtual void example_1();
virtual void example_2();
};

example.c

void Example :: example_1()
{
cout << "1";
}
void Example :: example_2()
{
cout << "2";
}

纯虚函数必须在派生类中实现,我已经这样做了。但我仍然不清楚为什么会出现此错误

错误“无法分配抽象类型‘Example’的对象:example.h 27:7: 注意:因为以下虚函数在 'Example' 中是纯函数:"

main.c:225:25:

error: cannot allocate an object of abstract type 'Example'
In file included from main.c:328:0:
example.h:27:7: note: because the following virtual functions are pure within 'Example':
base.h:150:18: note: virtual void Base::example_1()
base.h:151:18: note: virtual void Base::example_2()

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com