[英]Forward declaration of a template class c++
I saw similar examples, but didn't understand them fully so please don't mark this as duplicate straight away. 我看到类似的例子,但并没有完全理解他们,所以请不要标记这个作为重复立竿见影。 I think there's a simple solution for my problem, and I'm only learning C++
. 我认为有一个简单的解决方案可以解决我的问题,而我只学习C++
。
I want to use: 我要使用:
template<class T, std::size_t N>
class arnas_array {
//a copy of std:array functionality, basically, here.
};
in another class header, another file, example: 在另一个类的头,另一个文件,例如:
class options_databaze {
public:
struct options_to_save{
arnas_array<char, 123> option_name;
//char option_name[103];
int * option_value_pointer;
};
};
And I can't get it to work. 而且我无法正常工作。 Forward declaration like this won't work: 像这样的正向声明将无法工作:
template<class T, std::size_t N>
class arnas_array;
I don't know much about this problem, first time I'm stuck here, any examples are gold. 我不知道很多关于这个问题,我第一次在这里被卡住,任何例子是金。
error C2079: 'options_databaze::options_to_save::option_name' uses undefined class 'arnas_array<char,123>'
The question has nothing to do with templates. 这个问题与模板无关。 In C++ a class type T
must be complete, in particular, if a non-static class data member of type T
is declared (see 3.2/5 (One definition rule)
section of the standard, or read more human-readable version here ). 在C ++中,类类型T
必须是完整的,特别是如果声明了类型T
的非静态类数据成员(请参阅标准的3.2/5 (One definition rule)
部分,或在此处阅读更多易于理解的版本 ) 。
"Must be complete" means that the definition of the class T
should precede the definition of the corresponding data member. “必须完整”表示类别T
的定义应在相应数据成员的定义之前。 A common way to achieve this, as was pointed out by Cameron in the comments, is to put a definition in a header file and include that header everywhere it's needed - just the same way as you do when you include standard headers such as <vector>
, <map>
and so on. 正如Cameron在评论中所指出的,实现此目标的一种常见方法是在头文件中放置一个定义,并在需要的地方包含该头文件-与包含标准头文件(如<vector>
, <map>
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.