-1

I am kinda new to C++, therefore, I don't know what is the cause of this error, I am trying to solve the edit distance problem recursively, however, this error shows up.

error: called object type 'int' is not a function or function pointer return __comp(__b, __a) ? __b : __a;

    #include <iostream>
#include <string>
#include <vector>

using namespace std;

int distance(string, string, int, int);
int cost(string, string, int, int);
int main(){
    string v = "love";
    string v2 = "hate";
    cout<<distance(v, v2, v.size()-1, v2.size()-1);

    
}

int distance(string v, string v2, int a, int b)
{
    if (a==0) return b;
    if (b==0) return a;

    return min(
        distance(v, v2, a, b-1)+1,
        distance(v, v2,  a-1, b)+1,
        distance(v, v2, a-1, b-1)+cost(v, v2, a, b));

}
int cost(string v, string v1, int a, int b)
{
    if (v[a]==v1[b]) return 0;
    return 1;
}
  • 1
    `std::min(a, b, c)` -> `std::min({a, b, c})`. – Jarod42 Sep 29 '21 at 09:21
  • 1
    BTW, also don't forget to include `` if you're going to use `std::min(...)`. – Ruks Sep 29 '21 at 09:23
  • I recommend writing `using std::min;` rather than `using namespace std;`. See also [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Stef Sep 29 '21 at 09:24
  • [OT]: pass your (unmodified) string by const reference instead of by value to avoid unneeded copies. – Jarod42 Sep 29 '21 at 09:26
  • Programming by guessing language rules is not a good idea. Why do you think `min(a, b, c)` should work? – Evg Sep 29 '21 at 09:31

1 Answers1

0

3rd argument of std::min is the comparer, you might prefer the overload taking initializer_list:

return min({
        distance(v, v2, a, b - 1) + 1,
        distance(v, v2,  a - 1, b) + 1,
        distance(v, v2, a - 1, b - 1) + cost(v, v2, a, b)});
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Or call min twice: `return min(distance(v, v2, a, b-1)+1, min(distance(v, v2, a-1, b)+1, distance(v, v2, a - 1, b - 1) + cost(v, v2, a, b)))` – Stef Sep 29 '21 at 09:23
  • 1
    @Stef: It is less clear IMO, so unless OP has to work pre-C++11, I won't use that. – Jarod42 Sep 29 '21 at 09:25