模板专业化:非内联函数定义问题
以下代码可以正确编译。
#include <string>
template <typename T, typename U>
class Container
{
private:
T value1;
U value2;
public:
Container(){}
void doSomething(T val1, U val2);
};
template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
; // Some implementation
}
template <>
class Container<char, std::string>
{
private:
char value1;
std::string value2;
public:
Container(){}
void doSomething(char val1, std::string val2)
{
; // Some other implementation
}
};
但是,如果我尝试在外部定义 void doSomething(char val1, std::string val2)
,则会出现以下错误。
#include <string>
template <typename T, typename U>
class Container
{
private:
T value1;
U value2;
public:
Container(){}
void doSomething(T val1, U val2);
};
template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
; // Some implementation
}
template <>
class Container<char, std::string>
{
private:
char value1;
std::string value2;
public:
Container(){}
void doSomething(char val1, std::string val2);
};
template<>
void Container<char,std::string>::doSomething(char val1, std::string val2)
{
; // Some other implementation
}
错误:
错误 1 错误 C2910: '容器::doSomething' : 不能明确 专门的 c:\users\bharani\documents\visual 工作室 2005\项目\模板\模板 专业化\templatespecializationtest.cpp 35
我犯了什么错误?
谢谢。
The following code compiles properly.
#include <string>
template <typename T, typename U>
class Container
{
private:
T value1;
U value2;
public:
Container(){}
void doSomething(T val1, U val2);
};
template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
; // Some implementation
}
template <>
class Container<char, std::string>
{
private:
char value1;
std::string value2;
public:
Container(){}
void doSomething(char val1, std::string val2)
{
; // Some other implementation
}
};
But if I try to define void doSomething(char val1, std::string val2)
outside, I get the following error.
#include <string>
template <typename T, typename U>
class Container
{
private:
T value1;
U value2;
public:
Container(){}
void doSomething(T val1, U val2);
};
template<typename T, typename U>
void Container<typename T, typename U>::doSomething(T val1, U val2)
{
; // Some implementation
}
template <>
class Container<char, std::string>
{
private:
char value1;
std::string value2;
public:
Container(){}
void doSomething(char val1, std::string val2);
};
template<>
void Container<char,std::string>::doSomething(char val1, std::string val2)
{
; // Some other implementation
}
Error:
Error 1 error C2910:
'Container::doSomething'
: cannot be explicitly
specialized c:\users\bharani\documents\visual
studio
2005\projects\templates\template
specialization\templatespecializationtest.cpp 35
What mistake do I commit?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有明确专门化成员函数。但是您正在定义显式(类模板)专业化的成员函数。这是不同的,您需要像这样定义它
请注意,“内联”很重要,因为这不是模板,并且如果它是在类外部定义的,则它不是隐式内联的。如果将标头包含到多个翻译单元中,则需要内联以避免重复的链接器符号。
您的显式专业化中是否有模板,则必须使用您的语法:
您的第一个代码中也有错误。您需要像这样定义类外定义,类模板的参数列表中没有“typename”
You are not explicitly specializing a member function. But you are defining the member function of an explicit (class template-) specialization. That's different, and you need to define it like
Note that the "inline" is important, because this is not a template and it is not implicitly inline if it is defined outside the class. It's needed to be inline to avoid duplicate linker symbols if you include the header into more than one translation unit.
Would you have a template in your explicit specialization, your syntax would have to be used:
You also have an error in your first code. You need to define the out of class definition like this, without "typename" in the argument list of the class template