| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 852 人关注过本帖
标题:为什么这个不行呢,有关位域问题。
只看楼主 加入收藏
林浩
Rank: 2
等 级:论坛游民
帖 子:47
专家分:23
注 册:2011-1-12
结帖率:90.91%
收藏
已结贴  问题点数:5 回复次数:4 
为什么这个不行呢,有关位域问题。
主要是switch语句出了问题, 只执行default语句了,不知道怎么回事,vc++6.0中。#include<iostream>
using namespace std;

enum Sys {LOW, HIGH};  //系统位数
enum Core {ONE, TWO, FOUR};//cpu单,双,四核
enum Thread {YES, NO}; //是否支持多线程

class Cpu  //Cpu类定义
{
    public:
        Cpu():frequ(0), system(LOW), core(ONE), thread(YES){}
        Cpu(int fre, Sys sys, Core co, Thread th):frequ(fre), system(sys), core(co), thread(th){}
        void show(void);
    private:
        unsigned int frequ : 12;
        Sys system : 1;
        Core core : 2;
        Thread thread : 1;
};

void Cpu::show(void)
{
    cout<<"the cpu information is:"<<endl;
    cout<<"frequency(MHz):"<<frequ<<endl;

    cout<<"the system is:"<<endl;
    switch(system)
    {
        case LOW:
                    cout<<"32bit"<<endl;
                    break;
        case HIGH:
                    cout<<"64bit"<<endl;
                    break;
        default:
                    cout<<"information error!"<<endl;
    }

    cout<<"the core is"<<endl;
    switch(core)
    {
        case ONE:
                cout<<"single core"<<endl;
                break;
        case TWO:
                cout<<"double cores"<<endl;
                break;
        case FOUR:
                cout<<"double double cores"<<endl;
                break;
        default:
                cout<<"information error!"<<endl;
    }

    cout<<"the cpu support over thread:"<<endl;
    switch(thread)
    {
        case YES:
                cout<<"yes"<<endl;
                break;
        case NO:
                cout<<"no"<<endl;
                break;
        default:
                cout<<"information error!"<<endl;
    }
}

int main(void)
{
    Cpu c(2000, HIGH, FOUR, NO);

    c.show();

    cout<<"the size of the class is:"<<sizeof(Cpu)<<endl;

    return 0;
}
2011-09-16 08:15
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
        unsigned int frequ : 12;
        Sys system : 1;
        Core core : 2;
        Thread thread : 1;
改为
        unsigned int frequ : 12;
        unsigned int system : 1;
        unsigned int core : 2;
        unsigned int thread : 1;

2011-09-16 09:53
林浩
Rank: 2
等 级:论坛游民
帖 子:47
专家分:23
注 册:2011-1-12
收藏
得分:0 
回复 2楼 rjsp
还有个问题,就是字节数测出来为什么是4呢,应该是2吧。
2011-09-16 12:23
林浩
Rank: 2
等 级:论坛游民
帖 子:47
专家分:23
注 册:2011-1-12
收藏
得分:0 
回复 2楼 rjsp
为什么要那样改呢, 那几个不是枚举类型变量吗, 为什么定义为unsigned int呢?
2011-09-16 12:26
daniel1986
Rank: 1
等 级:新手上路
帖 子:6
专家分:5
注 册:2011-8-31
收藏
得分:0 
不知道是不是这样改 这样改能显示正常
#include<iostream>
using namespace std;

enum Sys {LOW, HIGH};  //系统位数
enum Core {ONE, TWO, FOUR};//cpu单,双,四核
enum Thread {YES, NO}; //是否支持多线程

class Cpu  //Cpu类定义
{
public:
    Cpu():frequ(0), system(LOW), core(ONE), thread(YES){}
    Cpu(int fre, Sys sys, Core co, Thread th):frequ(fre), system(sys), core(co), thread(th){}
    void show();
private:
    int frequ ;//: 12;
    int system ;//: 1;
    int core ;//: 2;
    int thread ;//: 1;
};

void Cpu::show(void)
{
    cout<<"the cpu information is:"<<endl;
    cout<<"frequency(MHz):"<<frequ<<endl;   
    cout<<"the system is:"<<endl;
    switch(system)
    {
    case 0:
        cout<<"32bit"<<endl;
    break;
    case 1:
            cout<<"64bit"<<endl;
        break;
        default:
            cout<<"information error!"<<endl;
    }
    cout<<"the core is"<<endl;
    switch(core)
    {
    case ONE:
        cout<<"single core"<<endl;
        break;
    case TWO:
        cout<<"double cores"<<endl;
        break;
    case FOUR:
        cout<<"double double cores"<<endl;
        break;
    default:
        cout<<"information error!"<<endl;
    }
   
    cout<<"the cpu support over thread:"<<endl;
    switch(thread)
    {
    case YES:
        cout<<"yes"<<endl;
        break;
    case NO:
        cout<<"no"<<endl;
        break;
    default:
        cout<<"information error!"<<endl;
    }
}

int main()
{
    Cpu c(2000, HIGH, FOUR, NO);
   
    c.show();
   
    cout<<"the size of the class is:"<<sizeof(Cpu)<<endl;
   
    return 0;
}
2011-09-16 14:10
快速回复:为什么这个不行呢,有关位域问题。
数据加载中...
 
   



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

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