C++ c+中的模板继承错误+;
我有两个头文件A.h(包括一个纯虚拟函数)和B.h A.h: 我怎样才能解决这个问题?C++ c+中的模板继承错误+;,c++,templates,inheritance,header-files,C++,Templates,Inheritance,Header Files,我有两个头文件A.h(包括一个纯虚拟函数)和B.h A.h: 我怎样才能解决这个问题? 谢谢唯一的方法是使用转发声明: #ifndef __A_H__ #define __A_H__ template< typename > class B; template <class T> class A { virtual B<T>* f()=0; }; #endif \ifndef\uu A\H__ #定义__ 模板B类; 模板 甲级 { 虚拟B*f(
谢谢唯一的方法是使用转发声明:
#ifndef __A_H__
#define __A_H__
template< typename > class B;
template <class T>
class A
{
virtual B<T>* f()=0;
};
#endif
\ifndef\uu A\H__
#定义__
模板B类;
模板
甲级
{
虚拟B*f()=0;
};
#恩迪夫
您遇到了一个只能解决的问题。您能解释一下为什么抽象基类需要声明一个返回派生类实例的虚拟函数吗?我知道你的代码只是一个简单的例子,但我不明白你为什么需要这个。似乎您希望采用不同的方法。下面的链接逻辑相同。注意:带有双下划线的名称(如
\uu A\u H\uu
)保留用于实现(即编译器和标准库)。因此,通过另一个公约是一个好主意。
#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