1

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

Community
  • 1
  • 1
FJC
  • 166
  • 12
  • `push_back(n)` is pushing an *array*, not an `int`. Likewise with `w` and e and s – WhozCraig Aug 20 '14 at 11:14
  • 2
    you have another error : you need to add a "break" statement between each switch cases, otherwise you will have duplicates neighbors. Also, neighbors accept a list of int, and s[2] is ab array. – lucasg Aug 20 '14 at 11:15
  • You don't clear the incoming list first, is that ok? Also unless you need to insert/remove in the middle of neighbours later, I suggest vector. – Neil Kirk Aug 20 '14 at 11:16
  • 1
    Maybe you should use a `std::list>`, neighbours seem to be pairs of `x,y` coordinate instead of a plain `int`. – tgmath Aug 20 '14 at 11:18
  • In Python, supposing `n` is an empty list, `n.append((1,2))` would result in `[(1,2)]`, not `[1,2]`. In C++, the compiler catches your error before it happened. – molbdnilo Aug 20 '14 at 11:20
  • Simpler (and legal) logic instead of the switches would be: `if ( x != 0 ) neighbours.push_back(s);` `if ( x != xSIZE-1 ) neighbours.push_back(n);` – M.M Aug 20 '14 at 11:25
  • @tgmath is there any reason to prefer `tuple` over `pair` ? – M.M Aug 20 '14 at 11:26
  • A `vector` is probably a better container choice than `list` here, most operations are faster – M.M Aug 20 '14 at 11:27
  • And off on a tangent, you said you want to return a `list`, but make the function void and send in a references. Don't be afraid to have non-void return types in C++. – doctorlove Aug 20 '14 at 11:29
  • @MattMcNabb I don't think so. `pair` seems to be the better choice. – tgmath Aug 20 '14 at 11:46

3 Answers3

2

Variable s is defined as an array of two elements

int s[2]={x+1,y};

While the value type of the list has type int

list<int>& neighbours

You are trying to push_back the array instead of an object of type int

case 0: {neighbours.push_back(s);}

I think that in any case the function has no sense at least due to this strange statement

case xSIZE-1: {neighbours.push_back(n);}

case labels shall be constant expressions.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

s is array (int[]), and neighbours.push_back take single int! Use in example neighbours.push_back(s[0]) or neighbours.push_back(s[1]) or anything, what is int.

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
0

A naive thing to do is add each neighbour on one line at a time. We can't use a non constant expression like xSIZE in a switch statement. This makes the code very long and it's crying out to be refactored into something neater, but it's a start. I'd also return the list rather than pass it in by reference:

list<int> 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};
  if(x == 0)
  {
    neighbours.push_back(s[0]);
    neighbours.push_back(s[1]);
  }
  else if (x == xSIZE-1)
  {
    neighbours.push_back(n[0]);
    neighbours.push_back(n[1]);
  }
  else
  {
    neighbours.push_back(s[0]);
    neighbours.push_back(s[1]);
    neighbours.push_back(n[0]);
    neighbours.push_back(n[1]);
  }

  if(y == 0)
  {
    neighbours.push_back(e[0]);
    neighbours.push_back(e[1]);
  }
  else if (y == ySIZE-1)
  {
    neighbours.push_back(w[0]);
    neighbours.push_back(w[1]);
  }
  else
  {
    neighbours.push_back(e[0]);
    neighbours.push_back(e[1]);
    neighbours.push_back(w[0]);
    neighbours.push_back(w[1]);
  }
  return neighbours;
}
doctorlove
  • 18,872
  • 2
  • 46
  • 62