Visual C ++中的奇怪枚举错误 [英] Strange Enum Error in Visual C++

查看:65
本文介绍了Visual C ++中的奇怪枚举错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试为我的游戏创建枚举并使用它时,出现此错误代码.
错误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",一切都会好起来的. :-)



你为什么写一些你不理解的东西.您的typedef没有定义类型,它只是为该类型引入了一个新名称,即别名:
Remove this "typedef" and you will be fine. :-)



Why are you writing something which you do not understand. Your typedef does not define a type, it simply introduces a new name for the type, an alias:
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 [


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.

—SA


这篇关于Visual C ++中的奇怪枚举错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆