加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

但是当我编译它给我这样的错误
在B.h中包含的文件中:4,
?????????????????来自Test.cpp:1:
A.h:10:错误:ISO C禁止声明’B’没有类型
A.h:10:错误:’B’被声明为’虚拟’字段
A.h:10:错误:预期’;’在’<'标记之前

#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问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读