【问题标题】:c++ cross initialization jump to case label when initializing a float arrayc ++初始化浮点数组时交叉初始化跳转到案例标签
【发布时间】:2016-10-20 08:07:36
【问题描述】:

我有 3 个类 M , L , P ,每个都有自己的 .header 和 .cpp 用于封装实践我已经分别在每个 .header 和 .cpp 中放置了相关的功能和属性

我在 L.cpp 中有这个函数float L::calculate(arg,arg,arg,arg)here,它只是一个固定公式,其参数取自类本身。但是我还有一个班级P

在 P.cpp 中,我有 L 类的计算函数的获取/设置。在类 P 中,我也有构造函数,1 个默认值和 1 个接受 args(我也在两个构造函数中初始化 L)

然后在我的 M 类中,所有实现都聚集在一起。

我正在尝试创建一个像这样的浮点数组:

float calculateThis[size]; // the size is fixed at 50.

然后在 x 小于 size 的 for 循环中

我这样做了:

calculateThis[x] = newL[x].caculate(args,args,args,args);
newP.setCalculate(calculateThis[x]);
++x;

我也在上面声明了

L newL[size]; //Used for some other methods that i have wrote to get inputs   and save it .
P newP[size];

当我编译我得到这个错误:

  crosses initialization of ‘float calculateThis[size]’
   float calculateThis[size];

基本上我正在尝试将返回浮点数的计算函数保存到浮点数组中

编辑:

    switch(choice)
    {
    case 1: // 1st Choice.
    while(entry<size)
    {
      //getting user input and saving it by newL.set / newP.set     
    };
    break;

    case 2:
    float calculateThis[size]; // the size is fixed at 50.
  for(x=0,x<size)
  {       calculateThis[x] = newL[x].caculate(newL.get(),newL.get(),newL.get(),newL.get());
          newP.setCalculate(calculateThis[x]);
           ++x;
  }
    break;

    default:break;  
    }

【问题讨论】:

  • "可以转移到块中,但不能通过初始化绕过声明的方式。从具有自动存储持续时间的局部变量不在的点跳转的程序除非变量具有 POD 类型 (3.9) 并且在没有初始化程序的情况下声明,否则范围到它在范围内的点是格式错误的。”向我们展示更多代码。这可能是您搞砸了case 声明。
  • 检查编辑@GillBates,我添加了一些代码

标签: c++ arrays linux ubuntu g++


【解决方案1】:

使用方括号表示这是calculateThis 的本地范围:

    case 2:
    {
      float calculateThis[size]; // the size is fixed at 50.
      for(x=0,x<size)
      {       
        calculateThis[x] = newL[x].caculate(newL.get(),newL.get(),newL.get(),newL.get());
          newP.setCalculate(calculateThis[x]);
           ++x;
      }
     }break;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-09
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2010-11-05
    • 1970-01-01
    相关资源
    最近更新 更多