Murmu Dev
Murmu Dev

Reputation: 1

Unable the find the error but the compiler gives the following message:

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]);
    }
  }
}


Upvotes: 0

Views: 92

Answers (4)

Vimal Rai
Vimal Rai

Reputation: 847

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))

Upvotes: 1

user11422223
user11422223

Reputation:

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];

Upvotes: 3

Lukas-T
Lukas-T

Reputation: 11340

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++ ;)

Upvotes: 1

adrien bedel
adrien bedel

Reputation: 116

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

Upvotes: 0

Related Questions