简体   繁体   中英

Error C2440: 'return' : cannot convert from 'int [2]' to 'int (&&)[2]'

I haven't programmed in a while, so my code might be a bit sloppy. The only thing the program does is create a 4x4 bool grid with only the top left value true. It then runs it with the checkAdjacentTiles, that should return the tiles touching it (the one to the right and the one underneath). I get an error instead. I have a feeling this has to do with my vector: std::vector<int[2]> checkAdjacentTiles(bool[4][4]); , since the int [2]. Thanks for the help!

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

I can't use int[2] in a vector for some reason. I ended up using std::pair<int,int> instead and it works fine. Thanks jhnnslschnr.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM