1

I am currently working on a game, where I am using the turn-based game api from apple's game center. I have been trying to minimize the size of my data i'm sending over Game Center by using c structs.

I have one struct containing data for a turn, and one that holds information about what the current round is and it also has an array containing all the other turns.

However I wanted the array to be dynamic, so I easily can Iterate through it, and I decided to use c++ vectors, which didn't work.

Here is my code so far:

The header:

.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;

Test implementation:

.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);
    }
}

When I run this, I get an error saying "Lexical or Preprocessor error. 'vector' file not found"

Any help is much appreciated.

1 Answer 1

1

Yes, but only in Objective-C++ mode. Which means that, for starters, you need to rename your source file (any any other source files that include this header) to have an extension of .mm.

Sign up to request clarification or add additional context in comments.

2 Comments

I did that. Actually I don't know why it doesn't work, but got it to work by using preprocessor if statements. Like #ifdef __cpluplus
You don't actually need to rename the file. You can always set the type in the File Inspector for the file. Of course .mm makes it easier to understand by looking at the filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.