c++ typedef结构中的指针数组

omvjsjqw  于 2023-03-05  发布在  其他
关注(0)|答案(1)|浏览(142)

我试图创建一个指针列表,让我可以访问所有创建的对象。

typedef struct global {

    int gameEntityCount;
    Entity*  gameEntities[]; // Line 17
    
    int nextValidSpot();
} global;

1>Entity.cpp 1>d:\source\repos\game\maybegame\game.h(17): error C2143: syntax error: missing ';' before '*' 1>d:\source\repos\game\maybegame\game.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 1>d:\source\repos\game\maybegame\game.h(17): error C2238: unexpected token(s) preceding ';'
有人能帮忙吗?
在此处初始化:

Map::Map()
{
    // Initialize global
    gameGlobal = new global();

    // Set map id (always 0) and address (to get pointer later on)
    m_iEnt = 0;
    gameGlobal->gameEntities[m_iEnt] = &m_pEnt;
    m_szClassname = "map";

    // Increase count to allow other entities to come in
    gameGlobal->gameEntityCount++;

    std::cout << "Constructor Map";
}
tnkciper

tnkciper1#

你真的应该

struct global {
    
    std::vector<std::shared_ptr<Entity>>  gameEntities;    
    int nextValidSpot();
} ;
  • 使用std::vector获得一个安全的动态调整数组大小的方法
  • 使用shared_ptr来获取安全指针

在c++中有很多复杂的东西,但是你必须知道,字符串,向量,Map,列表,shared_ptr和unique_ptr

相关问题