在Managed C ++ 2005中声明枚举的正确方法?

时间:2022-09-06 19:25:50

If I use /clr:oldSyntax the following should work:

如果我使用/ clr:oldSyntax,则以下内容应该有效:

public __value enum IceCreamFlavors
{
   Vanilla,
   Chocolate,
   Sardine,
};

what is the equivalent in non-oldSyntax? How do I declare a "managed" enum in Managed C++ for .NET 2.0?

非oldSyntax中的等价物是什么?如何在Managed C ++ for .NET 2.0中声明“托管”枚举?

Edit: when I follow JaredPar's advice, then if I try to pass an IceCreamFlavor to a function with the signature:

编辑:当我按照JaredPar的建议时,如果我尝试将IceCreamFlavor传递给具有签名的函数:

OrderFlavor(IceCreamFlavors flav)

by running

OrderFlavor(IceCreamFlavors::Sardine)

I get the error:

我收到错误:

'IceCreamFlavors Sardine' : member function redeclaration not allowed

2 个解决方案

#1


Try

enum class IceCreamFlavors {
  Vanilla,
  Chocolate,
  Sardine,
};

#2


Are you, by any chance, trying to declare your enum inside another class? ie:

你有没有机会尝试在另一个班级里宣布你的枚举?即:

public ref class Icecream
{
     public enum class flavours
     {
          Mint,
          Vanilla,
          Guac
     };
};

If you are, I would guess that you need to move it out so that it is its own class instead of a nested one. (Does managed c++ allow nested classes?) My impression is that you used to be able to do it unmanaged style inside another class, but since its its own class now, you probably shouldn't be nesting them. I might be wrong. My knowledge of managed c++ and c# is kind of weak.

如果你是,我猜你需要把它移出来,这样它就是它自己的类而不是嵌套的类。 (托管的c ++是否允许嵌套类?)我的印象是你曾经能够在另一个类中执行非托管样式,但由于它现在是自己的类,你可能不应该嵌套它们。我可能错了。我对托管c ++和c#的了解有点弱。

#1


Try

enum class IceCreamFlavors {
  Vanilla,
  Chocolate,
  Sardine,
};

#2


Are you, by any chance, trying to declare your enum inside another class? ie:

你有没有机会尝试在另一个班级里宣布你的枚举?即:

public ref class Icecream
{
     public enum class flavours
     {
          Mint,
          Vanilla,
          Guac
     };
};

If you are, I would guess that you need to move it out so that it is its own class instead of a nested one. (Does managed c++ allow nested classes?) My impression is that you used to be able to do it unmanaged style inside another class, but since its its own class now, you probably shouldn't be nesting them. I might be wrong. My knowledge of managed c++ and c# is kind of weak.

如果你是,我猜你需要把它移出来,这样它就是它自己的类而不是嵌套的类。 (托管的c ++是否允许嵌套类?)我的印象是你曾经能够在另一个类中执行非托管样式,但由于它现在是自己的类,你可能不应该嵌套它们。我可能错了。我对托管c ++和c#的了解有点弱。