【问题标题】:Error when passing list by reference (C++)通过引用传递列表时出错(C++)
【发布时间】:2014-10-13 17:21:11
【问题描述】:

我正在尝试在 C++ 中编写一个子例程来获取方格上一个单元格的位置并返回其邻居列表,因此对于大多数单元格它将返回 4 个邻居,但边缘上的单元格只会有 3 个,角落里只有 2 个。我不是一个非常熟练的 C++ 程序员,所以我在这里查找了一些帮助,以通过引用传递列表 (C++ pass list as a parameter to a function)。

我得到这个错误:

In function ‘void findNeighbours(int, int, int, int, std::list<int, std::allocator<int> >&)’:

error: invalid conversion from ‘int*’ to ‘int’

这是我的代码:

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

导致错误的行似乎是这一行: 案例 0:{neighbours.push_back(s);}

但我看不出我在做什么与我查找的示例(上面链接)不同。

正如我所说,我不是最熟练的 C++ 编码器,所以请尽可能用简单的语言解释。我习惯使用 Python,所以我不太擅长使用指针等。

提前感谢您的帮助,

FJC

【问题讨论】:

  • push_back(n) 正在推送 array,而不是 int。 w 和 e 和 s 也是如此
  • 你还有一个错误:你需要在每个 switch case 之间添加一个“break”语句,否则你会有重复的邻居。此外,邻居接受 int 列表,s[2] 是 ab 数组。
  • 你不先清除传入列表,可以吗?另外,除非您以后需要在邻居中间插入/删除,否则我建议使用向量。
  • 也许你应该使用std::list&lt;std::tuple&lt;int,int&gt;&gt;,邻居似乎是一对x,y坐标而不是普通的int。
  • 在 Python 中,假设n 是一个空列表,n.append((1,2)) 将导致[(1,2)],而不是[1,2]。在 C++ 中,编译器会在错误发生之前捕获它。

标签: c++ list compiler-errors pass-by-reference


【解决方案1】:

一种天真的做法是一次将每个邻居添加到一行中。 我们不能在 switch 语句中使用像 xSIZE 这样的非常量表达式。 这使得代码很长,并且需要重构为更整洁的东西,但这是一个开始。 我也会返回列表而不是通过引用传递它:

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

【讨论】:

    【解决方案2】:

    变量s被定义为两个元素的数组

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

    虽然列表的值类型是 int 类型

    list<int>& neighbours
    

    您正在尝试 push_back 数组而不是 int 类型的对象

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

    我认为无论如何,至少由于这个奇怪的说法,这个函数没有意义

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

    case 标签应该是常量表达式。

    【讨论】:

      【解决方案3】:

      s 是数组 (int[]),neighbours.push_back 是单个 int!在示例中使用neighbours.push_back(s[0]) 或neighbours.push_back(s[1]) 或任何东西,int 是什么。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-26
        • 2011-01-17
        • 2014-07-12
        • 1970-01-01
        • 2021-08-15
        • 2018-06-29
        • 2020-04-01
        相关资源
        最近更新 更多