Visual C++: Compiler Error C4430

12,577

Solution 1

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

Solution 2

You need to do two things:

  • #include <string>
  • Change the type to const static std::string QUIT_GAME (adding std::)

Solution 3

#include <string>
...
const static std::string QUIT_GAME;

Solution 4

Missing the #include<string>

and it's std::string

Share:
12,577
Nick Heiner
Author by

Nick Heiner

JS enthusiast by day, horse mask enthusiast by night. Talks I've Done

Updated on July 18, 2022

Comments

  • Nick Heiner
    Nick Heiner almost 2 years

    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?

  • Adam Rosenfield
    Adam Rosenfield almost 14 years
    You should never put a using statement in a header file. It irreversibly pollutes the namespace of any file that includes.
  • Thomas Matthews
    Thomas Matthews almost 14 years
    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.
  • Tyler McHenry
    Tyler McHenry almost 14 years
    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
    Tyler McHenry almost 14 years
    Don't use using in header files. Very bad juju. Otherwise, great, comprehensive answer.