注册 登录
编程论坛 C++教室

枚举类型怎么用的啊?

tmac我偶像 发布于 2010-11-07 11:15, 2077 次点击

 #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)
我想请教一下枚举类型是怎么用的啊,   可以给个经典例子么  谢谢!
4 回复
#2
ljt2010-11-07 11:49
这代码怎么可能有用呢?你类里面有这个构造函数Person person("li",Male,520320); ????那还怎么去构造呢
#3
聋眼睛瞎耳朵2010-11-07 12:55
至于枚举类型,定义是像你那么定义的,用的话,只能作赋值、关系、算术运算。例子的话,你这个函数也是个例子,只不过你写的有问题。

只有当类的数据成员是基本数据类型的变量或数组时,才可以像你主函数那样直接构造,而你的name是定义的指针形式,这个时候要自己定义个构造函数才能实现构造,并且指针还要记得分配内存!你自己再定义个构造函数就行了!
#4
tmac我偶像2010-11-07 13:15
回复 2楼 ljt
恩  忘了写构造函数了
#5
tmac我偶像2010-11-07 13:16
回复 3楼 聋眼睛瞎耳朵
哦  谢谢哈
1