Visual Studio 2005下模版函数的使用,该如何解决
Visual Studio 2005下模版函数的使用
如下是我的程序,在VC6中运行良好,但是在VC8下却出错,请问如何解决:
// test1.h
#pragma once
#include <iostream>
using namespace std;
template <class Type>
Type min( Type a, Type b ) {
return a < b ? a : b;
}
///////////////////////////////////////////////////////////////
// test1.cpp
#include "test1.h "
int main() {
int a = 10, b = 20;
int ia = min <int> ( a, b );
cout < < ia < < endl;
double da = min( 10.4, 20.0 );
cout < < da < < endl;
return 0;
}
/////////////////////////////////////////////////////////////
在VC8下的报错如下:
test1.cpp
d:\workspace\visual studio 2005\console\test1\test1\test1.cpp(21) : error C2668: “min”: 对重载函数的调用不明确
d:\workspace\visual studio 2005\console\test1\test1\test1.cpp(13): 可能是“Type min <int> (Type,Type)”
with
[
Type=int
]
d:\program files\microsoft visual studio 8\vc\include\xutility(2951): 或“const _Ty &std::min <int> (const _Ty &,const _Ty &)”
with
[
_Ty=int
]
试图匹配参数列表“(int, int)”时
生成日志保存在“file://d:\Workspace\Visual Studio 2005\Console\test1\test1\Debug\BuildLog.htm”
test1 - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
怎么回事呢?指代不明确,而VC6为什么就没这个错误呢?请指点其区别以及写模版函数应注意的事项。
------解决方案--------------------
不要和STL库的东西重名!
如下是我的程序,在VC6中运行良好,但是在VC8下却出错,请问如何解决:
// test1.h
#pragma once
#include <iostream>
using namespace std;
template <class Type>
Type min( Type a, Type b ) {
return a < b ? a : b;
}
///////////////////////////////////////////////////////////////
// test1.cpp
#include "test1.h "
int main() {
int a = 10, b = 20;
int ia = min <int> ( a, b );
cout < < ia < < endl;
double da = min( 10.4, 20.0 );
cout < < da < < endl;
return 0;
}
/////////////////////////////////////////////////////////////
在VC8下的报错如下:
test1.cpp
d:\workspace\visual studio 2005\console\test1\test1\test1.cpp(21) : error C2668: “min”: 对重载函数的调用不明确
d:\workspace\visual studio 2005\console\test1\test1\test1.cpp(13): 可能是“Type min <int> (Type,Type)”
with
[
Type=int
]
d:\program files\microsoft visual studio 8\vc\include\xutility(2951): 或“const _Ty &std::min <int> (const _Ty &,const _Ty &)”
with
[
_Ty=int
]
试图匹配参数列表“(int, int)”时
生成日志保存在“file://d:\Workspace\Visual Studio 2005\Console\test1\test1\Debug\BuildLog.htm”
test1 - 1 个错误,0 个警告
========== 生成: 0 已成功, 1 已失败, 0 最新, 0 已跳过 ==========
怎么回事呢?指代不明确,而VC6为什么就没这个错误呢?请指点其区别以及写模版函数应注意的事项。
------解决方案--------------------
不要和STL库的东西重名!