简体   繁体   English

在C ++中传递结构数组 - 在被调用函数中不存在结构元素,如何正确传递向量?

[英]Passing an array of structures in C++ - structure elements don't exist in the called function, how to properly pass vectors?

I'm trying to make a game of sorts in c++ where the player has to move 3 '@' chars, or otherwise known in the game as bears. 我试图在c ++中进行各种游戏,其中玩家必须移动3'@'字符,或者在游戏中以其他方式称为熊。

I can't seem to access the contents of my structure that I created for various items in the program, such as bears and bombs. 我似乎无法访问我为程序中的各种项目创建的结构内容,例如熊和炸弹。 For example, the x coordinates and the symbol are inaccessible in the called function, so I'm assuming it is to do with my passed parameters and my vector not being initialised properly. 例如,x坐标和符号在被调用函数中是不可访问的,所以我假设它与我传递的参数有关,而我的向量没有被正确初始化。 Code is below: 代码如下:

const char BEAR('@');
const int  SIZEX(10);       //horizontal dimension
const int  SIZEY(6);        //vertical dimension

    struct Item {
    int x, y;
    char symbol;
};  
    vector<Item> bear(3); //there are supposed to be 3 bears

int main()
{
void initialiseGame(char g[][SIZEX], char m[][SIZEX], vector<Item>& b);

//local variable declarations 
char grid[SIZEY][SIZEX];    //grid for display
char maze[SIZEY][SIZEX];    //structure of the maze
initialiseGame(grid, maze, bear);   //initialise grid (incl. walls & bear)
//other irrelevant stuff

return 0;
}

void initialiseGame(char grid[][SIZEX], char maze[][SIZEX], vector<Item>& bear)
{ //initialise grid & place bear in middle
    void setInitialMazeStructure(char maze[][SIZEX], vector<Item>& bear);
    void setInitialDataFromMaze(char maze[][SIZEX], vector<Item>& bear);
    void updateGrid(char g[][SIZEX], const char m[][SIZEX], vector<Item>& bear);

    setInitialMazeStructure(maze, bear);        //initialise maze
    setInitialDataFromMaze(maze, bear); //initialise bear's position
    updateGrid(grid, maze, bear);       //prepare grid
}

void setInitialMazeStructure(char maze[][SIZEX], vector<Item>& bear)
{ //set the position of the walls in the maze
    //initialise maze configuration
    int initialMaze[SIZEY][SIZEX]   //local array to store the maze structure
        = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
            { 1, 2, 0, 0, 0, 0, 0, 0, 0, 1 },
            { 1, 2, 0, 1, 0, 0, 1, 0, 0, 1 },
            { 1, 2, 0, 1, 1, 0, 1, 0, 1, 1 },
            { 1, 1, 0, 0, 0, 1, 0, 0, 0, 1 },
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } };
    // with 1 for wall, 0 for tunnel, etc. 
    //copy into maze structure
    for (int row(0); row < SIZEY; ++row)    
        for (int col(0); col < SIZEX; ++col)
            switch (initialMaze[row][col])
            {
                case 0: maze[row][col] = TUNNEL; break;
                case 1: maze[row][col] = WALL; break;
                case 2: maze[row][col] = BEAR; break;
            }
}
void setInitialDataFromMaze(char maze[][SIZEX], const vector<Item>& bear) 
{ //extract bear's coordinates from initial maze info
    for (int row(0); row < SIZEY; ++row)
        for (int col(0); col < SIZEX; ++col)
            switch (maze[row][col])
            {
                case bear:
                {
                    bear.x = col;
                    bear.y = row;
                    maze[row][col] = TUNNEL;
                }
                break;
            }
}

This switch statement in setInitialiDateFromMaze() is where I'm getting the error with regards to the vector of Item, the error states that x & y as well as symbol do not exist as members of my struct Item. setInitialiDateFromMaze()中的这个switch语句是我得到关于Item的向量的错误的地方,错误表明x&y和符号不存在作为我的struct Item的成员。

for (int row(0); row < SIZEY; ++row)
    for (int col(0); col < SIZEX; ++col)
        switch (maze[row][col])
        {
            case bear:
            {
                bear.x = col;
                bear.y = row;
                maze[row][col] = TUNNEL;
            }
            break;
        }

What can I do to fix this error? 我该怎么做才能解决这个错误? Is my vector of 3 bears being passed correctly? 我的3只熊的矢量是否被正确传递?

std::vector doesn't have member data x and y. std :: vector没有成员数据x和y。 The way you are using your vector is therefore wrong. 因此,使用矢量的方式是错误的。 You should access x and y in the following manner: 您应该以下列方式访问x和y:

bear[i].x

Bound-checking: 绑定检查:

bear.at(i).x
case bear:

bear is declared as a: bear被宣布为:

vector<Item> bear(3);

The case value, in a switch , must be a single, literal constant value. switchcase值必须是单个文字常量值。 bear is a vector, and it is not clear what your intent is, but this is the reason for your compilation error. bear是一个向量,并不清楚你的意图是什么,但这是你的编译错误的原因。 Not to mention that, subsequently bear.x and bear.y also makes no sense, since bear is a vector , and it does not have either x , or y , as a class member. 更不用说,随后的bear.xbear.y也没有意义,因为bear是一个vector ,并且它没有xy作为类成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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