【问题标题】:Using enum and struct?使用枚举和结构?
【发布时间】:2018-02-12 04:50:43
【问题描述】:

我有一个关于在我的作业中使用enumstruct 的问题,我在理解上有点困难。

首先,这是我们老师给我们的代码示例。

enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
struct WeatherType
{    
    int avgHiTemp;
    int avgLoTemp;
    float actualRain;
    float recordRain;
};

然后作业说声明WeatherType 组件的一维数组类型WeatherListType,由MonthType 类型的值索引。当我尝试通过编码来做到这一点时:

typedef WeatherType WeatherListType[MonthType];

我收到了这些错误:

problem5.cpp:19:47: error: expected primary-expression before ']' token 
typedef WeatherType WeatherListType[MonthType];
                                           ^
problem5.cpp:21:2: error: 'WeatherListType' was not declared in this scope
WeatherListType yearlyWeather;

我只是对问题所在感到困惑,如果我走在正确的轨道上,我到底做错了什么?

【问题讨论】:

    标签: c++ struct enums typedef


    【解决方案1】:

    这里的问题是在

    typedef WeatherType WeatherListType[MonthType];
    

    括号内的 [] 必须是一个数字(它是元素的数量),但 MonthType 不是一个数字,而是一个类型。

    这里有两个主要选择:

    1. 只需使用 12。

    2. 添加另一个枚举项:enum MonthType {..., NOV, DEC, NUM_MONTHS};

      然后使用 NUM_MONTHS。

      这是枚举的常见技巧 - 因为它们从 0 开始并向上计数,NUM_MONTHS 将是第 13 个值,因此它得到值 12。

      您这样做的原因是,如果您添加另一个值,编译器会自动为您调整 NUM_MONTHS。我认为我们不太可能很快迎来新月份,所以在这种情况下,这并不重要。

    (旁注:数组总是由 C 中的整数索引。你不能用枚举索引一个。但是枚举可以转换为整数并返回,所以这不是问题)

    【讨论】:

    • 我认为我们不会很快迎来新月份 ..ha ..谁知道 ::)
    【解决方案2】:

    在这里,我给出了完整的程序以供您澄清,因为我没有提到我在输出中给出的输入值,因为在 12 个月的每个月中,必须给出 4 个数据总共 48 个数据。假设这些值是给定的。但是当你运行时,你必须给出所有 48 个值。在你使用 JAN 的地方它是 conatant 0,在 FEB 它是常数 1,依此类推,对于 DEC,它是常数 11。希望你明白这一点对你有用

    #include<iostream>
    using namespace std;
    enum MonthType {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
    struct WeatherType
     {    
        int avgHiTemp;
        int avgLoTemp;
        float actualRain;
        float recordRain;
     };
    int main()
     {
         WeatherType WeatherListType[12];
         for (int i=JAN;i<=DEC;i++)
           { 
              cout<<"\n High temperature :";
              cin>>WeatherListType[i].avgHiTemp;
              cout<<"\n Low temperature :";
              cin>>WeatherListType[i].avgLoTemp;
              cout<<"\n Actual Rain :";
              cin>>WeatherListType[i].actualRain;
              cout<<"\n Record Rain :";
              cin>>WeatherListType[i].recordRain;
          }
         cout<<"\n Month \t HiTemp\t LoTemp\t ActualRain\t RecordRain\n";
    
        for (int i=JAN;i<=DEC;i++)
          {
            cout<<i<<"\t"<<WeatherListType[i].avgHiTemp;
            cout<<"\t"<<WeatherListType[i].avgLoTemp;
            cout<<"\t"<<WeatherListType[i].actualRain;
            cout<<"\t"<<WeatherListType[i].recordRain<<"\n";
       }
    }
    
      OUTPUT
    
      High temperature :40 
    
      Low temperature :28                                                                                                          
    
      Actual Rain :34                                                                                                              
    
      Record Rain :56 
      .
      .
      .
     Month HiTemp  LoTemp  ActualRain  RecordRain                                                                                   
     0       40      28      34          56                                                                                            
     1       45      32      45          67             
     2       34      23      56          76                                                                                            
     3       34      32      45          43                                                                                            
     4       67      12      45          43                                                                                            
     5       78      65      45          32                                                                                            
     6       56      54      34          23                                                                                            
     7       45      34      23          23                                                                                            
     8       3       1       45          43                                                                                            
     9       34      45      23          23                                                                                            
     10      12      3       34          43                                                                                            
      11      56      54      43         34   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-04
      • 2016-11-26
      • 2015-09-23
      • 2019-02-21
      • 2019-02-07
      相关资源
      最近更新 更多