我试了好几次找问题在哪里,但我找不到任何thing.So,有人能帮我找到问题吗?为什么我看不到结果?
这似乎是个愚蠢的问题,但我对编程世界并不陌生:)
这是我的代码:
#include <iostream>
using namespace std;
// There is the declraction of all functions
float max();
float min();
// This is the main program
int main ( int argc, char ** argv )
{
// Here you can find max
max(504.50,70.33);
// Here you can find min
min(55.77, 80.12);
return 0;
}
// This is the max function
int max(float a, float b){
float theMax;
if (a>b) {
theMax = a;
cout <<theMax ;
}
else{
theMax = b;
cout << b;
}
return theMax;
}
// This is the min function
int min( float c, float d){
float theMin;
if (c >d ) {
theMin =c;
cout << theMin;
}
else {
theMin =d;
cout << theMin;
}
return theMin;
}发布于 2014-08-19 08:41:20
你给std::max和std::min打电话。这是因为您编写了using namespace std,并且在使用它们之前没有声明自己的min和max。(您确实声明了另外两个min和max函数,但这些函数都使用零参数,而不是两个)。所以,当编译器看到max(504.50,70.33);时,唯一的候选是std::max。
发布于 2014-08-19 08:52:13
您可以声明这些重载:
float max();
float min();它们是不带参数并返回float的函数。
你给我打电话
max(504.50,70.33);和
min(55.77, 80.12);这些函数需要两个doubles,并且可能返回或不返回任何内容。
它们匹配std::max和std::min,而不是您声明的原型。
然后你定义
int min( float c, float d){这也与你声明的原型不匹配。
换句话说,这些函数在main中是未知的,实际上被调用的函数是std::min和std::max。
不要使用using namespace std; -在键入时保存的内容在清晰性和调试中丢失。
您还应该重命名这些函数--重用标准库名称并不是个好主意。
https://stackoverflow.com/questions/25378197
复制相似问题