对函数的引用在 C++ 中是不明确的

reference to function is ambiguous in c++

下面是我的 C++ 代码。当我在 c++17 中编译以下代码时,我收到这样的错误“对‘更大’的引用不明确。有人可以帮我解决这个问题

#include <iostream>
using namespace std;

void greater(int num1, int num2, int num3);

int main()
{
int a, b, c; 

cout<<"Enter three numbers"<<endl;
cin>>a>>b>>c;

greater(a,b,c);

return 0;
}


void greater(int num1, int num2, int num3){

if(num1 > num2 && num1 > num3){
    cout<<num1<<" is the greatest number of them all";
}
else if(num2 > num1 && num2 > num3){
    cout<<num3<<" is the greatest number of them all";
}

else if(num3 > num2 && num3 > num1){
    cout<<num3<<" is the greatest number of them all";
}

else if(num1 == num2 && num1 == num3){
    cout<<"All the numbers are equal";
}

else{
    cout<<"There's an error, Check the inputs"<<endl;
}
}

如果您的代码很小或一次性情况,我只会添加 using namespace std;。通常不是好的做法,因为在这种情况下,C++ 标准库包含 std::greater()。因此,当您声明自己的版本并添加 using namespace std; 时,编译器不知道该使用哪个。

这里有一个简单的方法来解决这个问题(您应该将 main() 向下移动或向前声明更大):

#include <iostream>

// using namespace std; -- Don't use this

void greater(int num1, int num2, int num3);

int main()
{
    int a, b, c; 

    std::cout<<"Enter three numbers"<<std::endl;
    std::cin>>a>>b>>c;

    greater(a,b,c);

    return 0;
}

void greater(int num1, int num2, int num3)
{
    if(num1 > num2 && num1 > num3){
        std::cout<<num1<<" is the greatest number of them all";
    }
    else if(num2 > num1 && num2 > num3){
        std::cout<<num3<<" is the greatest number of them all";
    }

    else if(num3 > num2 && num3 > num1){
        std::cout<<num3<<" is the greatest number of them all";
    }

    else if(num1 == num2 && num1 == num3){
        std::cout<<"All the numbers are equal";
    }

    else{
        std::cout<<"There's an error, Check the inputs"<<std::endl;
    }
}