Nick Heiner
Nick Heiner

Reputation: 122630

Visual C++: Compiler Error C4430

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?

Upvotes: 3

Views: 15376

Answers (6)

Thomas Matthews
Thomas Matthews

Reputation: 57749

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

Upvotes: 7

yu yang Jian
yu yang Jian

Reputation: 7181

In my case, I have typo on #define, I reference to this article to create dll project, and correct is wrting followiung in header file:

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

but I have typo lose some character and different in #else #define:

#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIARY_API __declspec(dllimport)

after fixing both to the same works.

Upvotes: 0

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

Upvotes: 4

David Gladfelter
David Gladfelter

Reputation: 4213

try adding at the top:

#include <string>
using std::string;

Upvotes: 0

Alan
Alan

Reputation: 46903

Missing the #include<string>

and it's std::string

Upvotes: 2

fbrereto
fbrereto

Reputation: 35945

You need to do two things:

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

Upvotes: 8

Related Questions