问题描述
尝试为我的游戏创建枚举并使用它时,出现此错误代码.
错误C4430:缺少类型说明符-假定为int.注意:C ++不支持default-int"
项目类型:Win32空白项目
IDE:Visual Studio 2010
操作系统:Windows 7 32bit.
我的代码:
这是我的所有代码:
I get this error code when trying to make an enum for my game and use it.
"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int"
Project Type: Win32 Blank Project
IDE: Visual Studio 2010
OS: Windows 7 32bit.
My Code:
Here''s all my code:
//===============================================
#include "DragonFireSDK.h"
//===============================================
GameState gameState = MainMenu;
//
// All of the below errors are for the above line of code.
//
//Error 1 error C2146: syntax error : missing ';' before identifier
//Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
//Error 4 error C2065: 'MainMenu' : undeclared identifier
//
int MainMenu_StartGame_Btn;
int MainMenu_Background_Img;
int View;
//===============================================
enum GameState
{
MainMenu,
LevelSelect,
DifficultySelect,
CountDown,
GamePlaying,
GameEnd,
Options,
HighScores,
Credits
};
struct Player
{
char Name[20];
int Score;
};
//===============================================
void AppMain()
{
LandscapeMode();
gameState = MainMenu;
MainMenu_Background_Img = ImageAdd("Images\\Background.png");
View = ViewAdd(MainMenu_Background_Img,0,0);
}
//===============================================
void OnTimer()
{
switch(gameState)
{
case MainMenu:
break;
case LevelSelect:
break;
case DifficultySelect:
break;
case CountDown:
break;
case GamePlaying:
break;
case GameEnd:
break;
case Options:
break;
case HighScores:
break;
case Credits:
break;
}
}
//===============================================
void AppExit()
{
}
//===============================================
推荐答案
GameState gameState = MainMenu;
您正在声明类型为GameState
的变量,但尚未告诉编译器GameState
是什么.您需要先定义类型,然后再尝试使用它.我还建议您仔细阅读 Sergey Alexandrovich [ ^ ].
You are declaring a variable of type GameState
but you have not told the compiler what a GameState
is. you need to define the type before you try to use it. I would also recommend you read carefully the advice provided by Sergey Alexandrovich[^].
typedef type_definition new_alias_name_for_this_type
//for example:
typedef int my_integer_type_I_want_to_use;
完成后,您可以使用my_integer_type_I_want_to_use
代替int
;这样做的目的是:您可以快速更改使用int
的决定.例如,如果您决定改用unsigned int
,则可以在定义my_integer_type_I_want_to_use
的一行中完成.
就您而言,enum MyType { A, B }
已经是一个完整的类型定义.如果您在之前使用typedef
(可以,但是为什么呢?),则此结构的末尾需要一个别名类型名称,该名称会丢失.现在清楚了吗?
请参阅:
http://en.wikipedia.org/wiki/Typedef [ ^ ].
最初的问题是关于enum
声明的:OP尝试在enum
之前使用冗余的typedef
对其进行定义.在我的第一个答案之后,问题就改变了.
After you do it, you can use my_integer_type_I_want_to_use
instead of int
; and the purpose if this is: you can quickly change your decision to use int
. For example, if you decide to use unsigned int
instead, it can be done in one line where you define my_integer_type_I_want_to_use
.
In your case, enum MyType { A, B }
is already a complete type definition. If you use typedef
before it (you could, but why?), this constructs needs an alias type name at the end, which is missing. Is it clear now?
Please see:
http://en.wikipedia.org/wiki/Typedef[^].
Original question was about enum
declaration: OP tried to define it with redundant typedef
before enum
. After my first answer, the question was changed.
这篇关于Visual C ++中的奇怪枚举错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!