c++中模板继承错误
template inheritance error in c++
我有两个头文件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
你有一个循环依赖的问题,只能使用前向声明来解决。
相关文章:
- 多级继承错误 C2280:"atttempting to reference a deleted function"
- 谷神星求解器成本函数继承错误:模板可能不是虚拟的
- C 预期的类名称{token,一个继承错误
- C++ 继承:错误:候选人需要 1 个参数,提供 0
- 继承错误:'{'标记之前的预期类名
- C++类多重继承错误
- 用模板类中的C 继承错误
- 在堆栈上分配对象时发生C++继承错误
- C++嵌套继承错误"expected class-name before ‘{’ token"
- 继承:错误:聚合类型A的类型不完整,无法定义
- 多重继承错误'invalid use of incomplete type'
- 继承错误 - 无法实例化抽象类 无法访问在类中声明的受保护成员
- C++ 继承错误:不明确的错误
- 类继承错误:私有成员
- 继承错误
- C++构造函数继承错误
- c++中的继承:错误C2011
- 多级继承错误"cout does not name a type"
- c++中模板继承错误
- c++类继承错误