views:

168

answers:

5

The code of Game.h:

#ifndef GAME_H
#define GAME_H

class Game
{
    public:
        const static string QUIT_GAME; // line 8
        virtual void playGame() = 0;
};

#endif

The error:

game.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(8): error C2146: syntax error : missing ';' before identifier 'QUIT_GAME'
game.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

What am I doing wrong?

+4  A: 
#include <string>
...
const static std::string QUIT_GAME;
BlueRaja - Danny Pflughoeft
You should never put a `using` statement in a header file. It irreversibly pollutes the namespace of any file that includes.
Adam Rosenfield
@Adam: fixed~~~
BlueRaja - Danny Pflughoeft
+7  A: 

You need to do two things:

  • #include <string>
  • Change the type to const static std::string QUIT_GAME (adding std::)
fbrereto
Actually one more thing: initialize the variable. The `const` specifier is the kicker here. If the variable is not initialized, it will be a constant empty string. This has uses, but I don't think it is what the OP wanted.
Thomas Matthews
Note that you cannot initialize a string (even a constant static string) inside the class declaration. There must be a separate definition statement (of the form `const std::string Game::QUIT_GAME = "Whatever";`) inside the corresponding source (.cpp) file. Edit: Just saw your answer which explains this in detail below, and upvoted it.
Tyler McHenry
+2  A: 

Missing the #include<string>

and it's std::string

Alan
A: 

try adding at the top:

#include <string>
using std::string;
David Gladfelter
In a header?? Who up-voted this?
sbi
+2  A: 

Here is what you need to fix your issues:

1. Include the string header file:
#include <string>

2. Prefix string with its namespace: const static std::string QUIT_GAME;

or insert a using statement:

#include <string>
using std::string;

3. Allocate space for the variable
Since you declared it as static within the class, it must be defined somewhere in the code:
const std::string Game::QUIT_GAME;

4. Initialize the variable with a value Since you declared the string with const, you will need to initialize it to a value (or it will remain a constant empty string).:
const std::string Game::QUIT_GAME = "Do you want to quit?\n";

Thomas Matthews
Don't use `using` in header files. Very bad juju. Otherwise, great, comprehensive answer.
Tyler McHenry