Visual C++: Compiler Error C4430
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
(addingstd::
)
Solution 3
#include <string>
...
const static std::string QUIT_GAME;
Solution 4
Missing the #include<string>
and it's std::string
Nick Heiner
JS enthusiast by day, horse mask enthusiast by night. Talks I've Done
Updated on July 18, 2022Comments
-
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 almost 14 yearsYou should never put a
using
statement in a header file. It irreversibly pollutes the namespace of any file that includes. -
Thomas Matthews almost 14 yearsActually 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 almost 14 yearsNote 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 almost 14 yearsDon't use
using
in header files. Very bad juju. Otherwise, great, comprehensive answer.