简体   繁体   中英

IOS C++ forbids comparison between pointer and integer (Not using strings)

I included the "not using strings" part in the title because when I was searching to find an answer on here I saw this problem a lot but it was with people using strings and getting chars mixed up. This is not my issue.

Trying to call an element of a 2D vector and see if it equals a certain number. I'm getting the comparison between pointer/integer error and I don't understand why. Here's my code, any help would be really appreciated.

bool UsedInRow(vector< vector<int> > vec(int n, vector<int> (int n)), int row, int num)
{

  int n;

  for (int col = 0; col < n; col++)
  {
     if (vec[row][col] == num)
     {
         return true;
     }
  }
 return false;
}

Try this:

bool UsedInRow(const vector< vector<int> >& vec, int row, int num) { ... }

The expression you used vector< vector<int> > vec(int n, vector<int> (int n)) is actually a function pointer.

The compiler thinks that you are passing a function pointer.

Instead, pass the vector by reference:

bool UsedInRow(vector< vector<int> > &vec, int row, int num)

As the other answers point out, vec is a function pointer, not a vector.

You're being advised to change vec to a vector, but it's not clear that that's the right solution. There's no ambiguity in your declaration; vec is defined as a parameter of function pointer type.

If that's what you intended, you need to replace the reference to vec inside the function with a function call. For example, you might change

if (vec[row][col] == num)

to

if ((vec(42, something))[row][col] == num)

where something would itself have to be a function pointer, pointing to a function that takes an int argument and returns a vector<int> result.

The declaration of your UsedInRow function is quite complicated, and I can't tell how it's intended to be used.

If vec really should be just a vector, then you can make it one by deleting the (int n, vector<int> (int n)) part of the parameter declaration -- but then I'd have to wonder why you wrote it in the first place.

For a definitive answer, you'd need to explain to us what your UsedInRow function is supposed to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM