c中的模板继承错误
发布时间:2020-12-16 09:38:33 所属栏目:百科 来源:网络整理
导读:我有两个头文件A.h(包括一个纯虚函数)和B.h. A.h: #ifndef __A_H__#define __A_H__#include "B.h"template class Tclass A{ virtual BT f()=0;};#endif B.h: #ifndef __B_H__#define __B_H__#include "A.h"template class Tclass B : public A T { BT f(){
我有两个头文件A.h(包括一个纯虚函数)和B.h.
A.h: #ifndef __A_H__ #define __A_H__ #include "B.h" template <class T> class A { virtual B<T> f()=0; }; #endif B.h: #ifndef __B_H__ #define __B_H__ #include "A.h" template <class T> class B : public A <T> { B<T> f(){} }; #endif 但是当我编译它给我这样的错误 #include "B.h" int main() { return 0; } 我该怎么解决这个问题? 解决方法
唯一的方法是使用前向声明:
#ifndef __A_H__ #define __A_H__ template< typename > class B; template <class T> class A { virtual B<T>* f()=0; }; #endif 您有circular dependency的问题,只能解决using forward declare问题. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |