c++ - 错误 C2440 : 'return' : cannot convert from 'int [2]' to 'int (&&)[2]'

标签 c++ vector visual-studio-2013 std stdvector

我有一段时间没有编程了,所以我的代码可能有点马虎。该程序唯一做的就是创建一个 4x4 bool 网格,只有左上角的值为真。然后它使用 checkAdjacentTiles 运行它,它应该返回接触它的瓷砖(右边的一个和下面的一个)。我得到一个错误。我觉得这与我的 vector 有关:std::vector<int[2]> checkAdjacentTiles(bool[4][4]); , 自 int [2]。感谢您的帮助!

#include <stdio.h>
#include <vector>

std::vector<int[2]> checkAdjacentTiles(bool[4][4]);

int main() {
    bool grid[4][4];

    grid[0][0] = 1;
    std::vector<int[2]> temp = checkAdjacentTiles(grid);

    for (int i = 0; i < (int)temp.size(); i++) {
        printf("(%i, %i)\n", temp[i][0], temp[i][1]);
    }

    getchar();
    return 0;
}

std::vector<int[2]> checkAdjacentTiles(bool checkGrid[4][4]) {
    int relAdjacentSides[4][2] = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };
    std::vector<int[2]> adjacentSides;

    for (int x = 0; x < 4; x++) {
        for (int y = 0; y < 4; y++) {
            for (int i = 0; i < 4; i++) {

                if (x + relAdjacentSides[i][0] >= 0 && x + relAdjacentSides[i][0] < 4) {
                    if (y + relAdjacentSides[i][1] >= 0 && y + relAdjacentSides[i][1] < 4) {
                        if (!checkGrid[x + relAdjacentSides[i][0], y + relAdjacentSides[i][1]]) {

                            bool stop = 0;
                            for (int v = 0; v < (int)adjacentSides.size(); v++) {
                                if (adjacentSides[v][0] == x + relAdjacentSides[i][0] && adjacentSides[v][1] == y + relAdjacentSides[i][1]) {
                                    stop = 1;
                                    break;
                                }
                            }

                            if (!stop) {
                                adjacentSides.push_back({ x + relAdjacentSides[i][0], y + relAdjacentSides[i][1] });
                            }

                        }
                    }
                }

            }
        }
    }

    return adjacentSides;
}

最佳答案

我不能使用 int[2]由于某种原因在 vector 中。我最终使用了 std::pair<int,int>相反,它工作正常。谢谢 jhnnslschnr。

关于c++ - 错误 C2440 : 'return' : cannot convert from 'int [2]' to 'int (&&)[2]' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29157451/

相关文章:

对向量进行舍入,以使所有结果元素都不同

C# Vector<double>.CopyTo 比非 SIMD 版本快多少?

c# - Visual Studio 在停止调试器后保持句柄打开

mysql - 如何在 x64 位机器上的 Visual Studio 2012/2013 express 中连接到 MySQL 数据源

C++ STL - STL sort() 的第三个参数如何工作?

c++ - 如何正确格式化此表?

c++ - 查找图像轮廓质心

C# - 读取并行端口状态(简单的按钮开关)

arrays - std::arrays 的 std::vector 的比较函数

vb.net - 如何在鼠标悬停时添加图像工具提示?