VS说"Too few arguments...",但其他编译器给了我正确的输出?

VS says "Too few arguments...", but other compilers give me a correct output?

本文关键字:输出 其他 few Too arguments VS 编译器      更新时间:2023-10-16

我正在两个字符串之间进行编辑距离。它使用递归函数。在线编译器正在编译代码并给我一个 3 的输出,这是正确的,但 Visual Studio 说"函数调用中的参数太少"其他人可以帮忙吗?

我查看了其他线程,它们确实缺少参数,但我没有,但 VS 正在标记我的递归调用

#include<iostream> 
#include<string>
using namespace std;

int min(int x, int y, int z)
{
return min(min(x, y), z); // here VS flags error
}
int editDist(string str1, string str2, int m, int n)
{
if (m == 0) return n;

if (n == 0) return m;
if (str1[m - 1] == str2[n - 1])
    return editDist(str1, str2, m - 1, n - 1);
return 1 + min(editDist(str1, str2, m, n - 1),    
    editDist(str1, str2, m - 1, n),   
    editDist(str1, str2, m - 1, n - 1)
);
}

int main()
{
string str1 = "sunday";
string str2 = "saturday";
cout << editDist(str1, str2, str1.length(), str2.length());
return 0;
 }

问题是由于您的函数名称与标准最小函数std::min匹配

int min(int x, int y, int z){
    return min(min(x, y), z); // the compiler is getting confused over whether to 
    //call std::min which takes two parameters or user-defined min which 
    //takes three parameters
    }

更改函数名称,它应该可以正常工作。

既然你用std::min,就需要用#include <algorithm>

如果它适用于某些编译器,那是因为很幸运并且您使用的某些标头包含<algorithm>(可能是间接的(。