我可以在objective-c结构中使用动态数组,如std::vector吗

我可以在objective-c结构中使用动态数组,如std::vector吗,objective-c,vector,struct,objective-c++,Objective C,Vector,Struct,Objective C++,我目前正在开发一款游戏,使用苹果游戏中心提供的基于回合的游戏api。 我一直在尝试使用c结构来最小化通过Game Center发送的数据的大小 我有一个包含回合数据的结构,一个包含关于当前回合的信息,还有一个包含所有其他回合的数组 但是,我希望数组是动态的,所以我可以很容易地迭代它,并且我决定使用C++向量,但它不起作用。 以下是我目前的代码: 标题: .h #import <vector> typedef struct { int points; //Points th

我目前正在开发一款游戏,使用苹果游戏中心提供的基于回合的游戏api。 我一直在尝试使用c结构来最小化通过Game Center发送的数据的大小

我有一个包含回合数据的结构,一个包含关于当前回合的信息,还有一个包含所有其他回合的数组

但是,我希望数组是动态的,所以我可以很容易地迭代它,并且我决定使用C++向量,但它不起作用。 以下是我目前的代码:

标题:

.h
#import <vector>

typedef struct  {
    int points; //Points that the player got in this round
    int round; // the round number for this turn
    char* playerID; //Who's turn was this?
} TurnData ;

typedef struct {
    std::vector<TurnData>* turns; //Keep a list over all turns
    int currentRound; //What round is it now?
    int maxRounds; //How many rounds for this game?
} GameData;
.h
#进口
类型定义结构{
int points;//玩家在这一轮中得到的点数
int round;//此回合的整数
char*playerID;//轮到谁了?
}TurnData;
类型定义结构{
std::vector*回合;//保留所有回合的列表
int currentRound;//现在是哪一轮?
int maxRounds;//这个游戏有多少回合?
}游戏数据;
测试实施:

.m
+(void)test {
    GameData data;
    data.currentRound = 1;
    data.maxRounds = 2;
    TurnData turnData;
    turnData.round = data.currentRound;
    turnData.points = 100;
    turnData.playerID = "playerID";
    data.turns->push_back(turnData);

    GameData loadData = data;
    for (std::vector<TurnData>::iterator it = loadData.turns->begin(); it != loadData.turns->end(); ++it) {
        NSLog(@"Player: %c, points: %d, round: %d", it->playerID, it->points, it->round);
    }
}
.m
+(无效)试验{
游戏数据;
data.currentRound=1;
data.maxRounds=2;
TurnData TurnData;
turnData.round=data.currentRound;
turnData.points=100;
turnData.playerID=“playerID”;
data.turns->push_back(turnData);
GameData loadData=数据;
对于(std::vector::iterator it=loadData.turns->begin();it!=loadData.turns->end();++it){
NSLog(@“玩家:%c,点数:%d,回合:%d”,它->玩家ID,它->点数,它->回合);
}
}
当我运行此程序时,会出现一个错误,提示“词法或预处理器错误。找不到矢量文件”


非常感谢您提供的任何帮助。

是的,但只能在Objective-C++模式下使用。这意味着,对于初学者,您需要重命名源文件(包括此头的任何其他源文件)以使扩展名为
.mm
是,但仅在Objective-C++模式下。这意味着,对于初学者来说,您需要重命名源文件(包括此标题的任何其他源文件)以使扩展名为
.mm

,我做到了这一点。实际上我不知道为什么它不工作,但是通过使用预处理器if语句使它工作。比如#ifdef uu cplus实际上不需要重命名文件。始终可以在文件检查器中为文件设置类型。当然.mm通过查看文件名更容易理解。我做到了。实际上我不知道为什么它不工作,但是通过使用预处理器if语句使它工作。比如#ifdef uu cplus实际上不需要重命名文件。始终可以在文件检查器中为文件设置类型。当然.mm通过查看文件名更容易理解。