enum的问题!!!!!!请大家给个提示。谢谢

KDE 2007-10-07 10:41:46
最近无意中使用MSDN的时候,我发现枚举的定义好像与以往不同:
VS2005帮助中说明枚举可以申明如下:
enum [tag] [: type] {enum-list} [declarator]; // for definition of enumerated type
enum tag declarator; // for declaration of variable of type tag

我马上在VC(2005)中试了一下:

enum Test : unsigned char
{
TEST_VALUE1 = 255,
TEST_VALUE2
};

编译之后给出了如下警告:
d:\tmp\test2\test2\main.cpp(9) : warning C4340: 'TEST_VALUE2' : value wrapped from positive to negative value
d:\tmp\test2\test2\main.cpp(9) : warning C4309: 'initializing' : truncation of constant value
d:\tmp\test2\test2\main.cpp(9) : warning C4369: 'TEST_VALUE2' : enumerator value '256' cannot be represented as 'unsigned char', value is '0'

除了感叹VC8的强大检查能力之外,我没有在任何标准文档中找到关于枚举可以指定基本类型的。
而且我在msdn中并没有看见这个特性被称作VC的扩展。

小弟在此请问各位大侠,这个语法(特性)是什么时候加入标准的,在那个文档或书中有记载?

或者如果没有加入标准,那又是什么时候作为VC扩展特性的,有没有相关文档记载?

谢谢!!!
...全文
191 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ckt 2007-10-08
  • 打赏
  • 举报
回复
学习..
jixingzhong 2007-10-08
  • 打赏
  • 举报
回复
上述是标准常用方式。


引用自: MSDN
num [tag] [: type] {enum-list} [declarator]; // for definition of enumerated type
enum tag declarator; // for declaration of variable of type tag

Parameters

tag

The type name given to the enumeration.

type

The underlying type of the enumeration identifiers. See Remarks.

enum-list

List of the enumerators contained by the enumeration.

declarator

Declarator list specifying the names of the enumeration. See Overview of Declarators for more information.

RemarksRemarks

By default, the first enumerator has a value of 0, and each successive enumerator is one larger than the value of the previous one, unless you explicitly specify a value for a particular enumerator. Enumerators needn't have unique values within an enumeration. The name of each enumerator is treated as a constant and must be unique within the scope where the enum is defined. An enumerator can be promoted to an integer value. However, converting an integer to an enumerator requires an explicit cast, and the results are not defined if the integer value is outside the range of the defined enumeration.

type is the underlying type of the identifiers. This can be any scalar type, such as 【signed or unsigned versions】 of 【int, short, or long. bool or char】 is also allowed.
...

该方式定义,用VS2005
jixingzhong 2007-10-08
  • 打赏
  • 举报
回复
enum [tag] {enum-list} [declarator];
visame 2007-10-08
  • 打赏
  • 举报
回复
"这个语法(特性)是什么时候加入标准的,在那个文档或书中有记载?

或者如果没有加入标准,那又是什么时候作为VC扩展特性的,有没有相关文档记载?"
个人观点如下:
这个可能是VC8的编译器自己设定的。并没有加入标准。

这个就好像C89和C99中都没有bool这个变量类型,但是很多编译器都支持bool,这是编译器方便用户设定的。不属于标准范围之列。
欢迎大家批评指正!
wjvlangz 2007-10-07
  • 打赏
  • 举报
回复
没见过,纯接分
星羽 2007-10-07
  • 打赏
  • 举报
回复
c++标准文档里好像没有这个,只是提了下 Underlying Type of an Enum 的 default 是 int


具体不及的了
using System; using System.Collections.Generic; using System.Text; namespace Work { class Program { static void Main(string[] args) { string str = "********************************************************************************"; Console.WriteLine(str); Console.WriteLine("\t\t\t\t欢迎来到事业测试系统"); Console.WriteLine(str); Console.WriteLine("您做出选择:1、开始测试;2、退出系统"); Console.WriteLine("小提示您务必按要求输入1或2,否则程序将无法运行!"); ConsoleKeyInfo input; while (true) { input = Console.ReadKey(true); if (input.Key == ConsoleKey.D1) { Program myProgram = new Program(); myProgram.Test(); break; } if (input.Key == ConsoleKey.D2) { break; } } Console.WriteLine(); Console.WriteLine("谢谢参与!按任意键退出!"); //括号里的true不显示输入的内容 Console.ReadKey(true); } //定义一个Test方法 void Test() { Console.WriteLine(); Console.WriteLine("做题之前按照提示输入您的基本信息:"); UserInfo user = new UserInfo(); Console.WriteLine("输入姓名:"); user.name = Convert.ToString(Console.ReadLine()); Console.WriteLine("输入性别:男或女"); string s = Console.ReadLine(); if (s == "男") user.sex = Sex.male; else user.sex = Sex.female; Console.WriteLine("输入年龄:"); bool flog = false; //while (!flog) //{ // flog = Int32.TryParse(Console.ReadLine(), out user.age); // Console.WriteLine("您的输入有误,重新输入:"); //} do { try { flog = true; user.age = Int32.Parse(Console.ReadLine()); } catch { flog = false; Console.WriteLine("您的输入有误,重新输入正确的数字:"); } } while (!flog); //Console.WriteLine("您的姓名为:{0]", user.name); //定义一个数组存放评价结果 string[] Remark = new string[6]; Remark[0] = "20-29:您是内向的悲观者。人们认为你是一个害羞的、神经质的、优柔寡断的,是须人照顾、永远要别人为你做决、不想与任何事或任何人有关。他们认为你是一个杞人忧天者,一个永远看到不存在的问题人。有些人认为你令人乏味,只有那些深知你的人知道你不是这样的人。"; Remark[1] = "30-39:您是缺乏信心的挑剔者。你的朋友认为你勤勉刻苦、很挑剔。他们认为你是一个谨慎的、十分小心的人,一个缓慢而稳定辛勤工作的人。如果你做任何冲动的事或无准备的事,你会令他们大吃一惊。他们认为你会从各个角度仔细地检查一切之后仍经常决定不做。他们认为对你的这种反应一部分是因为你的小心的天性所引起的。"; Remark[2] = "40-49:您是以牙还牙的自我保护者。别人认为你是一个明智、谨慎、注重实效的人。也认为你是一个伶俐、有天赋有才干且谦的人。你不会很快、很容易和人成为朋友,但是是一个对朋友非常忠诚的人,同时要求朋友对你也有忠诚的回报。那些真正有机会了解你的人会知道要动摇你对朋友的信任是很难的,但相等的,一旦这信任被破坏,会使你很难熬过。"; Remark[3] = "50-59:您是平衡的中道。别人认为你是一个新鲜的、有活力的、有魅力的、好玩的、讲究实际的、而永远有趣的人;一个经常是群众注意力的焦点,但是你是一个足够平衡的人,不至於因此而昏了头。他们也认为你亲切、和蔼、体贴、能谅解人;一个永远会使人高兴起来并会帮助别人的人。"; Remark[4] = "60-69:您是吸引人的冒险家。别人认为你是一个令人兴奋的、高度活泼的、相当易冲动的个性;你是一个天生的领袖、一个做决定会很快的人,虽然你的决定不总是对的。他们认为你是大胆的和冒险的,会愿意试做任何事至少一次;是一个愿意尝试机会而欣赏冒险的人。因为你散发的刺激,他们喜欢跟你在一起。"; Remark[5] = "70-80:您是傲慢的孤独者。别人认为对你必须「小心处理」。在别人的眼中,你是自负的、自我中心的、是个极端有支配欲、统治欲的。别人可能钦佩你,希望能多像你一点,但不会永远相信你,会对与你更深入的来往有所踌躇及犹豫。"; //定义一个数组存放用户输入的信息 string[,] Questions = new string[10, 5]; Questions[0, 0] = "1.你何时感觉最好?"; Questions[0, 1] = "A、早晨"; Questions[0, 2] = "B、下午及晚上"; Questions[0, 3] = "C、晚饭后"; Questions[0, 4] = "D、深夜"; Questions[1, 0] = "2.你走路时是……"; Questions[1, 1] = "A、大步的快走"; Questions[1, 2] = "B、小步的快走"; Questions[1, 3] = "C、不快,仰着头"; Questions[1, 4] = "D、不快,低着头"; Questions[2, 0] = "3. 和人说话时,你……"; Questions[2, 1] = "A、手臂交叠的站着"; Questions[2, 2] = "B、双手紧握着"; Questions[2, 3] = "C、一只手"; Questions[2, 4] = "D、玩着你的耳朵、摸着你的下巴、或用手整理"; Questions[3, 0] = "4. 坐著休息时,你的……"; Questions[3, 1] = "A、两膝盖并拢"; Questions[3, 2] = "B、两腿交叉"; Questions[3, 3] = "C、两腿伸直"; Questions[3, 4] = "D、一腿卷在身下"; Questions[4, 0] = "5. 碰到你感到发笑的事时,你的反应是……"; Questions[4, 1] = "A、一个欣赏的大笑"; Questions[4, 2] = "B、笑着,但不大声"; Questions[4, 3] = "C、轻轻的咯咯的笑"; Questions[4, 4] = "D、羞怯的微笑"; Questions[5, 0] = "6. 当你去一个派对或社交场合时,你……"; Questions[5, 1] = "A、一个欣赏的大笑"; Questions[5, 2] = "B、笑着,但不大声"; Questions[5, 3] = "C、轻轻的咯咯的笑"; Questions[5, 4] = "D、羞怯的微笑"; Questions[6, 0] = "7. 当你非常专心工作时,有人打断你,你会"; Questions[6, 1] = "A、欢迎他"; Questions[6, 2] = "B、感到非常恼怒"; Questions[6, 3] = "C、以上两极端之间"; Questions[6, 4] = "D、无所谓"; Questions[7, 0] = "8. 下列颜色中,你最喜欢哪一颜色?"; Questions[7, 1] = "A、红或橘色"; Questions[7, 2] = "B、绿色"; Questions[7, 3] = "C、深蓝或紫色"; Questions[7, 4] = "D、白色"; Questions[8, 0] = "9. 临入睡的前几分钟,你在床上的姿势是…"; Questions[8, 1] = "A、仰躺,伸直"; Questions[8, 2] = "B、侧躺,微卷"; Questions[8, 3] = "C、头睡在一手臂上"; Questions[8, 4] = "D、被盖过头"; Questions[9, 0] = "10. 你经常梦到你在……"; Questions[9, 1] = "A、打架或挣扎"; Questions[9, 2] = "B、找东西或人"; Questions[9, 3] = "C、飞或漂浮"; Questions[9, 4] = "D、你的梦都是愉快的"; Console.WriteLine("认真选择!"); string[] result = new string[10]; int sum = 0; for (int a = 0; a < 10; a++) { Console.WriteLine("回答第{0}道题:", a + 1); for (int b = 0; b < 5; b++) { Console.WriteLine(Questions[a, b]); } Console.Write("选择:"); result[a] = Shuru(ConsoleKey.A, ConsoleKey.B, ConsoleKey.C, ConsoleKey.D).ToString(); Console.WriteLine(); if (result[a] == "A") sum += 8; if (result[a] == "B") sum += 6; if (result[a] == "C") sum += 4; if (result[a] == "D") sum += 2; } Console.WriteLine("{0}您好!您的评价结果为:\n", user.name); switch (sum / 10) { case 2: Console.WriteLine(Remark[0]); break; case 3: Console.WriteLine(Remark[1]); break; case 4: Console.WriteLine(Remark[2]); break; case 5: Console.WriteLine(Remark[3]); break; case 6: Console.WriteLine(Remark[4]); break; case 7: case 8: Console.WriteLine(Remark[5]); break; } } //定义一个方法存储用户输入的答案 public static char Shuru(ConsoleKey key1, ConsoleKey key2, ConsoleKey key3, ConsoleKey key4) { ConsoleKeyInfo a; do { a = Console.ReadKey(true); } while (a.Key != key1 & a.Key != key2 & a.Key != key3 & a.Key != key4); Console.WriteLine(a.Key.ToString()); return (char)a.Key; } } } //定义一个保存用户基本信息的结构 struct UserInfo { public string name; public Sex sex; public int age; } //定义一个枚举表示性别 enum Sex { male, female } //定义一个枚举表示用户的选择分为A、B、C、D //enum Choose //{ // A,B,C,D //}

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧