| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2077 人关注过本帖
标题:枚举类型怎么用的啊?
只看楼主 加入收藏
tmac我偶像
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-9-17
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:4 
枚举类型怎么用的啊?

 #include<iostream>
  using namespace std;
   
 
  class Person{
        public:
        char *name;
        enum sex{Male,Female};
        sex sex1;
        double id;
        void toString()
        {
             cout<<"name:"<<*name<<endl;
             cout<<"sex1:"<<sex1<<endl;
             cout<<"id:"<<id<<endl;
             }
            
        
        };   
  class Student: public Person
  {
        private:
         double no,score;
        public:
        void toString()
        {
           cout<<"name:"<<*name<<endl;
             cout<<"sex1:"<<sex1<<endl;
             cout<<"id:"<<id<<endl;   
             cout<<"no:"<<no<<endl;
              cout<<"score:"<<score<<endl;                              
               }
           };
        class Teacher: public Person
        {
         private:
         enum postion{Prof,AssociateProf,AssistProf};
         postion postion1;
        public:
        void toString()
        {
           cout<<"name:"<<*name<<endl;
             cout<<"sex1:"<<sex1<<endl;
             cout<<"id:"<<id<<endl;   
             cout<<"postion:"<<postion1<<endl;                                               
               }
           };
         int main()
         {
             Person person("li",Male,520320);                                                      
             person.toString();                                                      
             Student student("wang",Female,520321,19,89);                                                      
             student.toString();
             Teacher teacher("wu",Male,520250,Prof);                                                      
             teacher.toString();                                                      
             system("pause");
             return 0;
             }  
编译时  总是报错 C:\Users\Administrator\Desktop\作业2.cpp In function `int main()':
                 50 C:\Users\Administrator\Desktop\作业2.cpp `Male' undeclared (first use this function)
                   (Each undeclared identifier is reported only once for each function it appears in.)
                  52 C:\Users\Administrator\Desktop\作业2.cpp `Female' undeclared (first use this function)
                  54 C:\Users\Administrator\Desktop\作业2.cpp `Prof' undeclared (first use this function)
我想请教一下枚举类型是怎么用的啊,   可以给个经典例子么  谢谢!
搜索更多相关主题的帖子: 类型 枚举 
2010-11-07 11:15
ljt
Rank: 6Rank: 6
等 级:侠之大者
威 望:3
帖 子:191
专家分:431
注 册:2009-4-6
收藏
得分:5 
这代码怎么可能有用呢?你类里面有这个构造函数Person person("li",Male,520320); ????那还怎么去构造呢
2010-11-07 11:49
聋眼睛瞎耳朵
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:29
专家分:123
注 册:2010-9-24
收藏
得分:5 
至于枚举类型,定义是像你那么定义的,用的话,只能作赋值、关系、算术运算。例子的话,你这个函数也是个例子,只不过你写的有问题。

只有当类的数据成员是基本数据类型的变量或数组时,才可以像你主函数那样直接构造,而你的name是定义的指针形式,这个时候要自己定义个构造函数才能实现构造,并且指针还要记得分配内存!你自己再定义个构造函数就行了!
2010-11-07 12:55
tmac我偶像
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-9-17
收藏
得分:0 
回复 2楼 ljt
  忘了写构造函数了
2010-11-07 13:15
tmac我偶像
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2010-9-17
收藏
得分:0 
回复 3楼 聋眼睛瞎耳朵
  谢谢哈
2010-11-07 13:16
快速回复:枚举类型怎么用的啊?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027590 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved