每当我声明枚举时,它都不会编译

时间:2014-02-25 08:51:56

标签: c# visual-studio-2012 enums

每当我声明enum时,它就不会编译。我的代码如下所示:

private enum race {HUMAN, ORC, GOBLIN, UNDEAD}

// The name of the player
string playerName;

// The Health Points of the player. Is to be modified  a LOT. Keep that in mind!
int HP = 100;

// Made to test if the name chosen i the right one
bool nameIsRight = false;

cout("Hello fair traveler!\n\n");
cout("It has come to my knowledge, that you, a puny warrior, is up for the challenge, that is killing the dragon\n\n");

cout("Our records seems to be damaged. What was your name again?\n\n");
cout("Enter your name: ");

// Sets playerName equal to the line entered by the player, turned into a string to prevent errors
playerName = Console.ReadLine().ToString();

cout("\n");

do
{
    string test;
    cout("Are you sure that is the right name? (y/n)\n");
    test = Console.ReadLine();
    cout("\n");
    if (test.ToLower() == "y")
    {
        nameIsRight = true;
    }
    else if (test.ToLower() == "n")
    {
        cout("What is your name then?\n");
        playerName = Console.ReadLine();
    }
    cout("\n");
} while (nameIsRight == false);

cout("So, your name is " + playerName + "? That is a name i haven't heard in a long time!\n\n");
cout("Just for the record, we need your race. Hope you don't mind telling it again.\n\n");
cout("1: Human\n\n2: ");
  

错误1}预期C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 12 10 simpleRPGConsole

     

错误2方法必须有一个返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 25 13 simpleRPGConsole

     

错误3类型预期C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 25 18 simpleRPGConsole

     

错误4方法必须有一个返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 26 13 simpleRPGConsole

     

错误5类型预期C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 26 18 simpleRPGConsole

     

错误6方法必须具有返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 28 13 simpleRPGConsole

     

错误7预期类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 28 18 simpleRPGConsole

     

错误8方法必须有一个返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 29 13 simpleRPGConsole

     

错误9预期类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 29 18 simpleRPGConsole

     

错误10类,结构或接口成员声明中的令牌'='无效C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 32 24 simpleRPGConsole

     

错误11无效的标记'('在类,结构或接口成员声明中C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 32 42 simpleRPGConsole

     

错误12方法必须具有返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 32 45 simpleRPGConsole

     

错误13方法必须有一个返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 34 13 simpleRPGConsole

     

错误14预期类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 34 18 simpleRPGConsole

     

错误15方法必须具有返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 39 17 simpleRPGConsole

     

错误16预期类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 39 22 simpleRPGConsole

     

错误17类,结构或接口成员声明中的标记'='无效C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 40 22 simpleRPGConsole

     

错误18无效的标记'('在类,结构或接口成员声明中C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 40 40 simpleRPGConsole

     

错误19方法必须有一个返回类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 41 17 simpleRPGConsole

     

错误20预期类型C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 41 22 simpleRPGConsole

     

错误21无效标记'('在类,结构或接口成员声明中C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 42 33 simpleRPGConsole

     

错误22类,结构或接口成员声明中的标记'='无效C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 44 33 simpleRPGConsole

     

错误23命名空间不能直接包含字段或方法等成员C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 46 17 simpleRPGConsole

     

错误24命名空间不能直接包含字段或方法等成员C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 52 15 simpleRPGConsole

     

错误25类型或命名空间定义,或预期文件结束C:\ Users \ Herbstein \ documents \ visual studio 2013 \ Projects \ simpleRPGConsole \ simpleRPGConsole \ Program.cs 57 9 simpleRPGConsole

我该怎么做才能帮到你?我不想在没有枚举的情况下制作这样的东西!

黑尔布斯泰因

2 个答案:

答案 0 :(得分:6)

您没有显示整个代码,但您的enum似乎位于某个功能的中间。不要那样做。

此外,您的方法需要位于class。看看一些很好的例子。

此外,C#中不存在cout - 这是一个C ++的东西,并且你没有在任何语言中使用它。 C#使用Console.WriteLine

namespace MyApp
{
    // Every method (function) must be in a class
    class MyProgram
    {
        // This is where code execution begins
        static void Main()
        {
            Console.WriteLine("Hello, world!");

            string playerName = GetPlayerName();
        }

        static string GetPlayerName()
        {
            Console.WriteLine("Some cliche narrative here. Name?");

            return Console.ReadLine();
        }

        // We can also make nested class/struct/enum definitions that are
        // "private" to the containing class.
        private enum APrivateEnum { Foo, Bar }
    }

    enum Race { Human, Orc, Goblin, Undead }
}

答案 1 :(得分:1)

尝试在方法之外定义枚举。