0

The error message says: In functionvoid LCSlength(std::__cxx11::string, std::__cxx11::string, int, int)

Error:

expression cannot be used as a function lookup[ i ][ j ] = max (lookup[i - 1] [ j ],lookup[ i ] [ j - 1 ]);


#define max 20

int lookup[max][max];

void LCSlength(string x,string y,int m,int n)
{
  for(int i = 1;i<=m;i++)
  {
    for(int j = 1;j<=n;j++)
    {
      if(x[i - 1] == y[j - 1])
       lookup[i][j] = lookup[i - 1][j - 1] + 1;
       else
        lookup[i][j] = max(lookup[i - 1][j], lookup[i][j - 1]);
    }
  }
}


Reza Jenabi
  • 3,884
  • 1
  • 29
  • 34
Murmu Dev
  • 1
  • 1
  • if you have a function max which you havent shown for brevity, then you erase reference to it by "#define max 20" and the max function you may have declared elsewhere cant be accessed. Because once the preprocessor directive runs, the last line looks to the compiler like "lookup [i] [j] = 20(lookup[i-1][j] , lookup[i][j-1]);" You cant have a function called "20". Use an alternative label for "max" in the #define / array sizes, or create or rename the max function to something else – Stephen Duffy Apr 12 '20 at 08:50
  • 1
    What do you want `max` to be? A placeholder for `20` or the name of the function (or macro) which returns the maximum value between two values? You could use `MAX_SIZE` for the former, while the latter is `std::max`. – Bob__ Apr 12 '20 at 08:50

4 Answers4

3

Use std::max(lookup[i - 1][j], lookup[i][j - 1]); and replace your macro name by something else, say maximum:

#define maximum 20
int lookup[maximum][maximum];
1

You need to define the max function or use macro like this

#define MAX(a,b) ((a) > (b) ? (a) : (b))

refer to MIN and MAX in C

Edit Preprocessors are case sensitive and it is advised to keep them in capital letters. Just for the sake of correct answer, I am putting them in small caps. You can use a function as well.

#define max(a,b) ((a) > (b) ? (a) : (b))
Vimal Rai
  • 757
  • 5
  • 6
1

Solution

if you can use C++11 use

constexpr int max = 20;

or if you can't use C++11 use

const int max = 20;

And don't skip the namespace to avoid ambiguities:

lookup[i][j] = std::max(lookup[i - 1][j], lookup[i][j - 1]);

Explanation

You have a macro

#define max 20

Now the preprocessor is a quite stupid text-replacement tool, that will now replace every instance of "max" with "20". So you end up with

lookup[i][j] = 20(lookup[i - 1][j], lookup[i][j - 1]);

Which doesn't make any sense. Prime example why you shouldn't use macros in modern C++ ;)

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

The problem is that you want to use the "max" function of stl, but you declared a variable called max at the beginning. You should call std::max instead or change the name of your max variable. In general, avoid using std to avoid those mistakes

adrien bedel
  • 116
  • 8