I'm currently following Bruce Sutherland's "Learn C++ for Game Development." I'm using Xcode.
I've added the following code, but can't get it to compile:
main.cpp:
#include "Player.h"
#include "GameLoop.h"
int main (int argc, char * const argv[]) {
Player player;
GameLoop::WelcomePlayer(player);
bool isPlaying = true;
while(isPlaying) {
isPlaying = GameLoop::RunGame();
}
return 0;
}
Player.h:
#pragma once
#include <string>
struct Player {
std::string m_Name;
};
GameLoop.h:
#pragma once
#include "Player.h"
#include "PlayerOptions.h"
namespace GameLoop {
void WelcomePlayer(Player& player);
void GivePlayerOptions();
void GetPlayerInput(std::string& playerInput);
PlayerOptions EvaluateInput(std::string& playerInput);
bool RunGame();
}
GameLoop.cpp:
#include "GameLoop.h"
#include <iostream>
using namespace std;
namespace GameLoop {
void WelcomePlayer(Player& player) {
cout << "Welcome to Text Adventure!" << endl << endl;
cout << "What is your name?" << endl << endl;
cin >> player.m_Name;
cout << endl << "Hello, " << player.m_Name << "!" << endl;
}
void GivePlayerOptions() {
cout << "What would you like to do?" << endl << endl;
cout << "1: Quit" << endl << endl;
}
void GetPlayerInput(string& playerInput) {
cin >> playerInput;
}
PlayerOptions EvaluateInput(string& playerInput) {
PlayerOptions chosenOption = PlayerOptions::None;
if(playerInput.compare("1") == 0) {
cout << "You have chosen to quit!" << endl << endl;
chosenOption = PlayerOptions::Quit;
}
else {
cout << "I do not recognise that option. Try again!" << endl << endl;
}
return chosenOption;
}
bool RunGame() {
bool shouldEnd = false;
GivePlayerOptions();
string playerInput;
GetPlayerInput(playerInput);
shouldEnd = EvaluateInput(playerInput) == PlayerOptions::Quit;
return !shouldEnd;
}
}
PlayerOptions.h:
#pragma once
enum class PlayerOptions {
Quit,
None
};
When I try to compile, I get quite a few errors on the PlayerOptions.h file:
#pragma once
enum class PlayerOptions { !Expected identifier before 'class'
Quit, !ISO C++ forbids declaration of 'Quit' with no type
None !ISO C++ forbids declaration of 'None' with no type
}; !Expected ';' before '}' token
The code is exactly as it is in the book, except from an include in the main.cpp file. The book has:
#include "stdafx.h"
When I try to add this, I get a message stating that the file can't be found. I'm guessing it's supposed to be a local file, but I'm not sure what importance it has on the project.
If I make the enum class PlayerOptions public, it gets rid of most error messages, but it still complains about the same errors in the PlayerOptions.h file.
Does anyone have any ideas?
Many thanks.