c++中模板继承错误

template inheritance error in c++

本文关键字:继承 错误 c++      更新时间:2023-10-16

我有两个头文件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,
from Test.cpp:1:
A.h:10: error: ISO c++禁止声明没有类型的' B '
错误:' B '声明为' virtual '字段
A.h: 10:错误:预计";"& lt;"牌

#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

你有一个循环依赖的问题,只能使用前向声明来解决。