如何为非模板类定义模板方法?
我的编译器对我实现模板方法的方式不满意。它为这些实现提供了大量错误消息,例如“未定义类型T”。
这是我的第一个方法,它是在类块之外实现的:
class VectorConvertor
{
public:
// ...
template <class T>
static void ReverseVectorElements(std::vector<T> & Vector);
// ...
};
template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
std::vector<T>::size_type size = Vector.size();
T swap;
for (std::vector<T>::size_type i=0; i<size/2; i++)
{
swap = Vector.at(i);
Vector.at(i) = Vector.at(size-1-i);
Vector.at(size-1-i) = swap;
}
}
另一个是这个;这次该方法是在类内部实现的:
class ElementaryMath
{
public:
// ...
template <class T> static char sign(T num)
{
return (num >= static_cast<T>(0)) ? static_cast<char>(+1) : static_cast<char>(-1);
}
// ...
}
我的代码有什么问题吗,或者这只是编译器本身的问题?
集成开发环境编译器:Visual Studio 2010
My compiler is not happy with the way I implement my template methods. It gives tons of error messages like "undefined type T" for these implementations.
This is my first method, it is implemented outside of the class block:
class VectorConvertor
{
public:
// ...
template <class T>
static void ReverseVectorElements(std::vector<T> & Vector);
// ...
};
template <class T>
void VectorConvertor::ReverseVectorElements(std::vector<T> & Vector)
{
std::vector<T>::size_type size = Vector.size();
T swap;
for (std::vector<T>::size_type i=0; i<size/2; i++)
{
swap = Vector.at(i);
Vector.at(i) = Vector.at(size-1-i);
Vector.at(size-1-i) = swap;
}
}
Another one is this; this time the method is implemented inside the class:
class ElementaryMath
{
public:
// ...
template <class T> static char sign(T num)
{
return (num >= static_cast<T>(0)) ? static_cast<char>(+1) : static_cast<char>(-1);
}
// ...
}
Is there anything wrong with my code, or is this just a problem with the compiler itself?
IDE & Compiler: Visual Studio 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你的代码对我来说看起来没问题。但有一件事一直萦绕在我的脑海里。你能检查一下函数“sign”之前是否定义过吗?只需将鼠标悬停在其上即可。 C 运行时库使用“#define”关键字实现其某些函数,因此您之后无法定义具有相同名称的函数。
Your code looks OK to me. But there is one thing that got in my mind. Can you please check if the function "sign" is defined before? Simply hover your mouse on it. C runtime library implements some of its functions using the "#define" keyword and because of that you can't define a function with that same name afterwards.
您缺少一些
typename
和分号,但除此之外,代码看起来还不错。恕我直言,如果它仍然不起作用,那么是时候提交错误了。顺便说一句,交换代码最好使用 std::swap 来完成。
You're missing some
typename
s and semicolons, but otherwise, the code seems OK. IMHO it's time to file a bug if it still doesn't work.BTW, the swapping code would be better done with
std::swap
.在此输入名称:
typename here:
希望这有帮助!
Hope this helps !
您的代码在 VS2005 上编译时没有错误(除了 ElementaryMath 定义末尾缺少分号),因此您可能会看到编译器错误。
VS2010 SP1 已推出测试版 这里。可能有帮助,但显然它是测试版......
Your code compiles without error on VS2005 (apart from a missing semicolon at the end of the definition of ElementaryMath) so you may be looking at a compiler bug.
VS2010 SP1 is available in beta here. Might help, but obviously its beta...