c ++中的模板继承错误

3 c++ inheritance templates header-files

我有两个头文件啊(包括纯虚函数)和Bh

啊:

#ifndef __A_H__
#define __A_H__

#include "B.h"
template <class T>
class A
{
   virtual B<T> f()=0;
};

#endif
Run Code Online (Sandbox Code Playgroud)

Bh:

#ifndef __B_H__
#define __B_H__

#include "A.h"
template <class T>
class B : public A <T> 
{
  B<T> f(){}
};

#endif
Run Code Online (Sandbox Code Playgroud)

但是当我编译它时给出了我的错误,就像
Bh中包含的文件:4,
来自Test.cpp:1:
啊:10:错误:ISO C++禁止声明'B'没有类型
啊:10:错误:'B '宣称为'虚拟'字段
啊:10:错误:预期';' 在'<'标记之前

#include "B.h"

int main() {
 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我该怎么解决这个问题?谢谢

BЈо*_*вић 7

唯一的方法是使用前向声明:

#ifndef __A_H__
#define __A_H__

template< typename > class B;

template <class T>
class A
{
   virtual B<T>* f()=0;
};

#endif
Run Code Online (Sandbox Code Playgroud)

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