Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我不能从仅前向声明的结构继承吗?
例子:
struct A; class B : public A {};
我收到错误“基类未定义”。
你能帮我解决这个问题吗?
谢谢
你不能这样做。首先,当你从一个结构或类继承时,它的大小必须是已知的。但是如果结构只是前向声明,编译器就无法确定大小。并且编译器还需要知道超类有哪些成员,例如是否有任何virtual函数等。
virtual
当您从类派生时,您实际上必须包含基类标头。前向声明是不够的