简体   繁体   English

如何正确使用 c++ 中的结构?

[英]How to properly use structs in c++?

I have a data structure in a header file of a player and a goblin that looks like this:我在播放器和地精的 header 文件中有一个数据结构,如下所示:

#ifndef GLOBALVAR_H
#define GLOBALVAR_H

#include <iostream>
#include <windows.h>

struct Player {
    int x = 1, y = 1;
    int health = 100;
    int symbol = '@';
};

struct Goblin {
    int x, y;
    int health = 100;
    int symbol = 'G';
};

#endif

I also have a data structure in a header file for the screen that looks like this:我在 header 文件中也有一个数据结构,屏幕如下所示:

#ifndef SCREEN_H
#define SCREEN_H

#include <iostream>
#include <windows.h>

struct Screen {
    char screen[21][34] = {
        "#################################",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#...............................#",
        "#################################"
    };
};

#endif

Finally, I have the main and generate c++ files that look like this respectively:最后,我分别生成如下所示的 c++ 文件:

// main 'hub' for game

#include <iostream>
#include <windows.h>

#include "screen.h"
#include "generate.h"
#include "globalVar.h"

using namespace std;

bool running = true;

int main() {
    struct Player p;
    struct Screen s;

    while(running) {

        system("cls");
        
        s.screen[p.y][p.x] = p.symbol;

        draw();

        p.health--;

        system("pause>nul");
        
    }

    return 0;
}
// generates the map and draws the screen

#include <iostream>
#include <windows.h>

#include "screen.h"
#include "globalVar.h"

using namespace std;

int generate() {


    return 0;
}

int draw() {
    struct Screen s;
    struct Player player;

    for(int i = 0; i < 20; i++) {
        cout << s.screen[i] << endl;
    }
    for(int i = 0; i < 1; i++) {
        cout << "HP: " << player.health << endl;
    }

    return 0;
}

When I run main.cpp I want it to display a '@' symbol at 1, 1 of the array.当我运行 main.cpp 时,我希望它在数组的 1、1 处显示一个“@”符号。 When I run the code it instead displays a period in the place of it.当我运行代码时,它会显示一个句点来代替它。 What is happening here?这里发生了什么?

Image of what's being displayed: https://imgur.com/a/RiWoDJ5正在显示的图像: https://imgur.com/a/RiWoDJ5

You have created two different Screen s.您已经创建了两个不同的Screen The one in main() is completely different from the one in draw() , because they are in different scopes. main()中的那个与draw()中的完全不同,因为它们在不同的范围内。 You can pass a Screen as an argument to draw() to fix this.您可以将Screen作为参数传递给draw()来解决此问题。

You can also make draw() a function of Screen , as Nathan pointed out in the comments, and then call s.draw() in main() .正如 Nathan 在评论中指出的那样,您还可以使draw()成为Screen的 function ,然后在main()中调用s.draw() ) 。

Also, unlike in C, there is no need to put struct before every use of a struct .此外,与 C 不同,不需要在每次使用struct之前放置struct Simply put Screen s;简单地说Screen s; instead of struct Screen s;而不是struct Screen s;

Example of draw() taking a parameter of type Screen :采用Screen类型参数的draw()示例:


int draw(Screen s) {
    Player player;

    for (int i = 0; i < 20; i++) {
        cout << s.screen[i] << endl;
    }
    for (int i = 0; i < 1; i++) {
        cout << "HP: " << player.health << endl;
    }

    return 0;
}

Example of draw() in Screen : Screen中的draw()示例:


struct Screen {
    static char screen[21][34];

    int draw() {
        Player player; //think about passing this as a parameter instead, not making one every time, because this one is inaccessible to everyone except this function

        for (int i = 0; i < 20; i++) {
            cout << screen[i] << endl;
        }
        for (int i = 0; i < 1; i++) {
            cout << "HP: " << player.health << endl;
        }

        return 0;
    }

};

Then, call it like:然后,将其称为:

s.draw();

I would go a different route and use inheritance and constructors:我会 go 一条不同的路线并使用 inheritance 和构造函数:

    struct Item
    {
        int m_x, m_y; // Every item has a position.
        char m_symbol;
    
        Item (char symbol, int x_pos = 1, int y_pos = 1)
        : m_symbol(symbol), m_x(x_pos), m_y(y_pos)
        { ; }
    };
    
    struct Player : public Item  // A player is an item
    : Item('@', 1, 1)
    {
    };
    
    struct Goblin : public Item // A goblin is an item
    : Item ('G', 5, 4)
    {
    };
    
    std::vector<std::vector<Item *>> game_board;

//...
    Goblin * p_goblin = new Goblin;
    game_board[p_goblin->y][p_goblin->x] = p_goblin;

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

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