Advertisement

Code won't compile

Started by January 09, 2015 01:17 AM
9 comments, last by jpetrie 10 years ago

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.

Stdafx.h is an autogenerated file created when a project is initialized by the new project wizard in visual studio. You will probably ok just removing the declaration that tries to include it, as it just contains resource constants for windows specific resource files.

Let me know if this helps at all!

Edit: looks like it also controls use of precompiled windows headers, which you also won't be using... Still safe to remove.
Advertisement

Stdafx.h is an autogenerated file created when a project is initialized by the new project wizard in visual studio. You will probably ok just removing the declaration that tries to include it, as it just contains resource constants for windows specific resource files.

Let me know if this helps at all!

Ah right, I see. I didn't include it in my code anyway, so I'm still not sure where the problem lies.

Thanks anyway!

Looking at your source, it looks like playeroptions should likely be public. What exact errors does the compiler produce please?

Are you able to paste perhaps with some indication of what line they refer to?


enum class PlayerOptions { !Expected identifier before 'class'

enum classes weren't introduced until c++11, which may not be enabled by default. Make sure to set the language dialect and standard library to c++11 under the project settings:

vG5S1Uu.png

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Looking at your source, it looks like playeroptions should likely be public. What exact errors does the compiler produce please?

Are you able to paste perhaps with some indication of what line they refer to?

GameLoop.cpp - Line 26:


PlayerOptions chosenOption = PlayerOptions::None; !'None' is not a member of PlayerOptions

GameLoop.cpp - Line 30:


chosenOption = PlayerOptions::Quit;  "!Within this context" & "!From this location"

Not too sure what thatt's referring to.

PlayerOptions.h - Lines 3,4,5,6:


enum class PlayerOptions {   !same as earlier
	Quit,
	None
};

If I make PlayerOptions public:


public enum class PlayerOptions {
	Quit,
	None
};

I then get an error on GameLoop.h:


PlayerOptions EvaluateInput(std::string& playerInput);!'PlayerOptions' doesn't name a type

and in PlayerOptions.h:


public enum class PlayerOptions { !Expected unqualified-id before 'public'
	Quit,
	None
};
Advertisement
It looks like swiftcoder is probably right about c++11 support. have you tried enabling it and rebuilding?

I didn't consider this as i was looking at the code not compiler specifics and don't own a mac :)
If I make PlayerOptions public:

public enum class PlayerOptions {
	Quit,
	None
};

I then get an error on GameLoop.h:


PlayerOptions EvaluateInput(std::string& playerInput);!'PlayerOptions' doesn't name a type

and in PlayerOptions.h:


public enum class PlayerOptions { !Expected unqualified-id before 'public'
	Quit,
	None
};

You most probably don't have c++11 enabled. Swiftcoders solution should fix it.

As for the public keyword in front of the enum, that's invalid c++ syntax, which is why it doesn't work nor fix your problem. In C++, the keyword public (along with private and protected) can only be used inside a struct or class declarations, and for inheritance specification, iirc.

devstropo.blogspot.com - Random stuff about my gamedev hobby

I've fixed the issue. I never had those "C++ Language Dialect" options in Xcode, so I've updated Xcode to a later version and now all is well.

I didn't need to change those options, so it looks like this version of Xcode supports more C++11 features.

Thanks for all your help!


I never had those "C++ Language Dialect" options in Xcode, so I've updated Xcode to a later version and now all is well.

They seem to redesign that screen and rename half the options in every release. It's a little frustrating - I usually have to go hunt for all my usual settings each time I upgrade.

Glad you got it working!

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement