0

I am new to C++ and I am having a trouble using pointers in function.

int old = Grid[x][y];
int new_one = Grid[x][y-1];
Crush_or_not = move(&old , &new_one);
y--;

Here I have a 2d array called Grid. I want to call a function called 'move'.

bool move(int *old_location, int *new_location)
{
    if (*new_location == 1)
    {
        *new_location = 3;
        *old_location = 1;
        return true;
    }
    else
    {
        return false;
    }
}

But the compiler returns an error:

error: no match for call to ‘(std::string {aka std::basic_string}) (int*, int*)’

So I don't quite understand why this would happen, can anyone help me?

Yang
  • 7,712
  • 9
  • 48
  • 65
  • 2
    Is function `move` declared before its usage? – Cepheus Oct 21 '18 at 16:50
  • 1
    My *guess* (what you get with no mcve). Wherever the first snippet resides, there is a `std::string` called `move` that is hiding your member function. If `move` were an unknown member, that's the error you would get (unknown function/identifier blah). That's not what you're getting. – WhozCraig Oct 21 '18 at 16:55
  • 1
    Do you have `using namespace XXX;` in your code? (where XXX is anything). – Martin York Oct 21 '18 at 17:21
  • apart from the function call compilation error, this code might not do what you expect: by reading two values from the grid array into `int` locals, the values updated in `move` will be written to `old` and `new_one`, but not update the grid position they were originally taken from. – Cee McSharpface Oct 21 '18 at 17:30
  • does your code contain any unicode character yet? – ngbaanh Oct 21 '18 at 17:35
  • @Martin York Yes I do have using namespace std. – De Santa Michell Oct 21 '18 at 17:41
  • @Cepheus I didn't declare it. Instead, I directly write it before the main function. – De Santa Michell Oct 21 '18 at 17:44
  • @dlatikay Yes, I noticed that. If I directly use &Grid[x][y], will the value be written in the array? – De Santa Michell Oct 21 '18 at 17:46
  • What is the type of `Crush_or_not`? – PhoenixBlue Oct 21 '18 at 17:59
  • @PhoenixBlue Its a bool. – De Santa Michell Oct 21 '18 at 18:34
  • see: [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/q/1452721/14065) You are importing from the standard namespace a bunch of functions called `move()`. The compiler is probebaly getting confused on which one to use. Stop using `using namespace std`. Or simply change the name of `move()` to something unique like: `updateInNewLocationIfOne()` as this actually describes what it does. Using the name `move()` to do the above is very non intuitive as its not actually a move its a conditional move based on a test. Make your function name reflect that. – Martin York Oct 21 '18 at 18:46
  • @MartinYork Thanks! It works perfectly now. – De Santa Michell Oct 21 '18 at 19:08
  • @desantamichell [yes](https://stackoverflow.com/a/15985644/1132334) – Cee McSharpface Oct 21 '18 at 19:15
  • Thanks, everyone! This problem is solved! : ) – De Santa Michell Oct 21 '18 at 20:41

0 Answers0