[英]Defining and calling a C++ function of a specialized template
I am currently learning C++ in-depth, and I have come across something that has stumped for a couple hours now. 我目前正在深入学习C ++,而且我遇到了一些困扰了几个小时的东西。 Why is it when I make a template and then specialize it, that I can't call or define that function for the specialized version?
为什么当我制作模板然后专门化它时,我无法为专用版本调用或定义该函数? The compiler complains, and I have scoured Google for a possible hint as to what I am doing wrong, but to no avail.
编译器抱怨,我已经搜索谷歌可能提示我做错了什么,但无济于事。 I am very sure it is something very simple that I am overlooking:
我非常确定这是一个非常简单的东西,我忽略了:
template <typename T>
class C { };
//specialization to type char
template <>
class C <char>
{
public:
void echo();
};
//compiler complains here
template <>
void C <char> :: echo()
{
cout << "HERE" << endl;
}
error: template-id 'echo<>' for 'void C::echo()' does not match any template declaration
错误:'void C :: echo()'的template-id'echo <>'与任何模板声明都不匹配
//specialization to type char
template <>
class C <char>
{
public:
void echo();
};
//template<> <----- don't need to mention template<> here
void C <char> :: echo()
{
cout << "HERE\n";
}
Ps Never say endl
when you mean '\\n'
. 当你的意思是
'\\n'
时,永远不要说endl
。 What is the C++ iostream endl fiasco? 什么是C ++ iostream endl惨败?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.