I'm trying to write a subroutine in C++ to take the location of one cell on a square grid and return a list of its' neighbours, so for most cells it will return 4 neighbours, but cells on the edge will only have 3 and in the corner will only have 2. I'm not a very proficient C++ programmer, so I looked up some help on here for passing the list by reference (C++ pass list as a parameter to a function).
I get this error:
In function ‘void findNeighbours(int, int, int, int, std::list<int, std::allocator<int> >&)’:
error: invalid conversion from ‘int*’ to ‘int’
Here is my code:
void findNeighbours(int x, int y, int xSIZE, int ySIZE, list<int>& neighbours){
int n[2]={x-1,y};
int e[2]={x,y+1};
int s[2]={x+1,y};
int w[2]={x,y-1};
switch(x){
case 0: {neighbours.push_back(s);}
case xSIZE-1: {neighbours.push_back(n);}
default: {neighbours.push_back(n);neighbours.push_back(s);}
}
switch(y){
case 0: {neighbours.push_back(e);}
case ySIZE-1: {neighbours.push_back(w);}
default: {neighbours.push_back(e);neighbours.push_back(w);}
}
}
The line causing the error appears to be this line: case 0: {neighbours.push_back(s);}
but I can't see what I'm doing differently to the example I looked up (linked above).
As I said, I'm not the most proficient C++ coder, so please explain in a simple language as you can. I'm used to using Python so I'm not very good with pointers etc.
Thanks in advance for your help,
FJC