参考文献 C++另一个文件中的结构对象?

发布于 2024-10-25 05:22:45 字数 1170 浏览 4 评论 0原文

我正在尝试使正在进行的游戏更加模块化。我希望能够声明游戏中所有 room_t 对象的单个数组 (room_t rooms[]),将其存储在 world.cpp 中并从其他文件调用它。

下面的截断代码不起作用,但据我所知。我想我需要使用 extern 但一直无法找到正确工作的方法。如果我尝试在头文件中声明数组,则会收到重复对象错误(我假设每个文件都调用 world.h)。

main.cpp

#include <iostream>
#include "world.h"

int main()
{
    int currentLocation = 0;
    cout << "Room: " << rooms[currentLocation].name << "\n";
    // error: 'rooms' was not declared in this scope
    cout << rooms[currentLocation].desc << "\n";    
    return 0;
}

world.h

#ifndef WORLD_H
#define WORLD_H
#include <string>


const int ROOM_EXIT_LIST = 10;
const int ROOM_INVENTORY_SIZE = 10;

struct room_t
{
    std::string name;
    std::string desc;
    int exits[ROOM_EXIT_LIST];
    int inventory[ROOM_INVENTORY_SIZE];
};  

#endif

world.cpp

#include "world.h"

room_t rooms[] = {
  {"Bedroom", "There is a bed in here.", {-1,1,2,-1} },
  {"Kitchen", "Knives! Knives everywhere!", {0,-1,3,-1} },
  {"Hallway North", "A long corridor.",{-1,-1,-1,0} },
  {"Hallway South", "A long corridor.",{-1,-1,-1,1} }
};

I'm in the process of trying to make a game-in-progress more modular. I'd like to be able to declare a single array of all the room_t objects in the game (room_t rooms[]), store it in world.cpp and call it from other files.

The truncated code below does not work, but it's as far as I've gotten. I think I need to use extern but have not been able to find a method that works correctly. If I try and declare the array in the header file, I get a duplicate object error (as each file calls world.h, I'd assume).

main.cpp

#include <iostream>
#include "world.h"

int main()
{
    int currentLocation = 0;
    cout << "Room: " << rooms[currentLocation].name << "\n";
    // error: 'rooms' was not declared in this scope
    cout << rooms[currentLocation].desc << "\n";    
    return 0;
}

world.h

#ifndef WORLD_H
#define WORLD_H
#include <string>


const int ROOM_EXIT_LIST = 10;
const int ROOM_INVENTORY_SIZE = 10;

struct room_t
{
    std::string name;
    std::string desc;
    int exits[ROOM_EXIT_LIST];
    int inventory[ROOM_INVENTORY_SIZE];
};  

#endif

world.cpp

#include "world.h"

room_t rooms[] = {
  {"Bedroom", "There is a bed in here.", {-1,1,2,-1} },
  {"Kitchen", "Knives! Knives everywhere!", {0,-1,3,-1} },
  {"Hallway North", "A long corridor.",{-1,-1,-1,0} },
  {"Hallway South", "A long corridor.",{-1,-1,-1,1} }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

左耳近心 2024-11-01 05:22:45

只需在 world.h 文件中添加 extern room_t rooms[]; 即可。

Just add extern room_t rooms[]; in your world.h file.

灯下孤影 2024-11-01 05:22:45

世界.h

extern room_t rooms[];

world.h

extern room_t rooms[];
长安忆 2024-11-01 05:22:45

问题是您试图引用在 .cpp 文件中声明的变量。在该文件的范围之外没有对此进行处理。为了解决这个问题,为什么不在 .h 文件中声明变量但有一个 Init 函数:

room_t rooms[];
void Init();

然后在 .cpp 中

void Init() {
   // create a room_t and copy it over
}

The problem is that you're trying to reference a variable you've declared in the .cpp file. There's no handle on this outside of the scope of this file. In order to fix this, why not declare the variable in the .h file but have an Init function:

room_t rooms[];
void Init();

Then in the .cpp

void Init() {
   // create a room_t and copy it over
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文