【问题标题】:Whenever i declare an enum, it won't compile每当我声明一个枚举时,它都不会编译
【发布时间】:2014-02-25 08:51:55
【问题描述】:

每当我声明 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 Invalid token '(' in class, struct, or interface member declaration 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

Error 21 Invalid token '(' in class, struct, or interface member declaration 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

我应该做些什么来帮助这个?我不想在没有枚举的情况下做出这样的事情!

赫伯斯坦

【问题讨论】:

  • 从错误消息看来,代码是直接写在一个类中的,检查你所有的执行代码是否都在方法内,并且所有的括号都关闭了..

标签: c# visual-studio-2012 enums


【解决方案1】:

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

另外,您的方法需要在class 中。看看一些很好的例子。

另外,cout 在 C# 中不存在 - 那是 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。可悲的是,基本的“哎呀,阅读 C# 的基本语法”现实检查。
  • 我知道基本语法,只是不知道它应该是外部函数。非常感谢您的帮助!
  • @user3350284 好的。欢迎来到堆栈溢出。请记住对任何有帮助的答案进行投票,并接受解决您问题的答案。
  • @JonathonReinhart cout 是我自己定义的一个函数,以使语法对我来说更具可读性。我不是 C# 的完整初学者,但我主要是在 Unity 中进行原型设计。
  • @Herbstein 很公平。但是,我会提醒您不要编写此类函数以使其看起来像其他语言。如果您打算与任何人使用您的代码进行交互,通常最好学习一种语言的最佳实践并坚持下去。在这种情况下,这意味着坚持使用(诚然更冗长的)Console.WriteLine。只是一个建议,毕竟这是你的代码。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多