模板类专业化
我确实阅读了一些相关的线程,但问题仍然不清楚:
#include <stdio.h>
#include <vector>
#include <iostream>
template <> class stack <int>
{
public:
std :: vector <int> stackVector;
};
编译错误:
templateSpecializ.cpp:5: error: ‘stack’ is not a template
templateSpecializ.cpp:6: error: explicit specialization of non-template ‘stack’
从此链接: coderSource.net
我错过了一些要点吗?我感觉我有。我什至尝试在那里定义函数,但这没有帮助。
I did read some of the related threads but still the issue was not clear:
#include <stdio.h>
#include <vector>
#include <iostream>
template <> class stack <int>
{
public:
std :: vector <int> stackVector;
};
The compilation error:
templateSpecializ.cpp:5: error: ‘stack’ is not a template
templateSpecializ.cpp:6: error: explicit specialization of non-template ‘stack’
From this link: coderSource.net
Have I missed some point? I feel I have. I even tried to define the functions there, but that was not helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是称为堆栈的模板的模板特化。堆栈未在任何这些头文件中定义。如果您希望定义一个新的模板类,您必须首先定义基本情况
如果您希望只为 int 定义堆栈,而不是为您可以使用的每种类型定义堆栈
That is a template specialisation of a template called stack. stack is not defined inany of those header files. If you wish to define a new template class you must first define the base case
If you wish to only define stack for int and not for every type you can use
如果您还没有可以专门化的模板,则无法专门化您的模板。所以这应该有效:
You can not specialize your template if you don't have a template to specialize yet. So this should work: