如何在C++中转发声明模板类并将其用作成员数据
How to forward declare a template class and use it as a member data in C++?
我正试图转发声明一个模板类,然后使用该类在其他类中声明成员数据。代码如下:
using namespace std;
template<class T>
class B;
class A{
B<T> b;
};
template<class T>
class B{
T x;
};
int main(){
}
我得到了编译错误:
error: ‘T’ was not declared in this scope
B<T> b;
有人能告诉我我做错了什么以及如何实现我的目标吗?(我注意到SO上模板类的帖子,但没有一个回答我的问题。)
提前感谢!
这:
class A{
B<T> b;
};
应该是这样的:
template <class T>
class A{
B<T> b;
};
通过将B<T>
添加到A
中,基本上将A
变成了模板类型为T
的模板类,因此A
类删除也应该被临时化。
首先,class A
不是一个模板。因此,您必须使用类型(即,不是T
)专门化B
对象。其次,在成员变量的声明点(即b
),B
是一个不完整类型。因此,你只能有一个指向它的指针或引用
template<class T>
class B;
class A{
B<int> *b;
^^^ ^
};
template<class T>
class B{
T x;
};
或者,如果这不会引起任何影响。如果您想要一个具体的B
对象,请更改类A
和B
的定义顺序,因为在您的示例中B
与A
:无关
template<class T>
class B{
T x;
};
class A{
B<int> b;
};
编辑:
如果你不知道在类A中声明B时你将使用什么特殊类型的B(所以添加"是不可行的),那么你也必须将class A
作为模板。这样你就可以有一个具体类型的B
:
template<class T>
class B;
template<class T>
class A{
B<T> b;
};
template<class T>
class B{
T x;
};
编辑:
请看我对101010的评论和大卫的回答。基本上,我想知道是否有可能在C++中实现以下目标:前向声明模板类B,然后将其用作类a的成员数据B的类型,而无需(1)将a作为模板类,(2)关心在声明B时将使用什么特殊类型。
你的要求毫无意义。这不是你的错。你只是误解了C++的工作原理。让我向你解释一下。
远期申报
示例:
class Foo;
对于编译器来说,上面的语句意味着:"将在其他地方定义一个名为Foo的类"。从这一点开始,直到它的定义Foo是不完整类型。有些事情你可以用不完整的类型来做,有些事情你做不到。特别是:不能声明该类型的变量和成员变量(也称为字段)。示例:
class Foo;
class Bar0
{
Foo f; // syntax error: Foo is an incomplete type
};
void fun0(Foo f) // syntax error: Foo is an incomplete type
{
Foo f; // syntax error: Foo is an incomplete type
}
class Foo
{
int x;
Foo f; // syntax error: Foo is an incomplete type
void fun(Foo other) { // Ok here: see "note"
}
}; // Foo becomes complete here.
// Note: things are actually more complicated
// for example: Foo is complete inside it's own methods
// even if they are defined inside the definition of Foo.
class Bar1
{
Foo f; // Ok here: Foo is complete
};
void fun1(Foo f) // Ok here: Foo is complete
{
Foo f; // Ok here: Foo is complete
}
对不完整类型可以做的一件事就是声明一个指向它的指针
class Foo;
void fun(Foo* f) // Ok here
{
}
class Bar
{
Foo* f; // Ok here
};
模板
示例:
template<class Bar>
class Foo
{
Bar b;
};
模板类就像一个蓝图,可以用来创建许多类。要从模板创建类,您必须用具体值替换的参数。CCD_ 18和CCD_。
你可以做两件事:
待继续
进一步阅读:
定义和声明之间的区别是什么:https://stackoverflow.com/a/1410632/5420829
如何处理不完整类型:
https://stackoverflow.com/a/553869/5420829
- 用于访问容器<T>数据成员的正确 API
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 数据成员SFINAE的C++17测试:gcc vs clang
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 静态数据成员模板专用化的实例化点在哪里
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 将私有数据成员添加到野牛生成的类中
- 输入数据成员未按要求工作
- 二维矢量数据成员
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 调用在 HXX 文件中声明的静态数据成员
- 是否可以根据其数据成员的类型确定类型的大小
- 访问数据成员(本身是对象)的数据成员,就好像它们是类成员一样
- 使公共数据成员在C++中无法访问
- 从 XML 中读取未指定结构的每个数据成员