无效使用非静态数据成员错误

时间:2019-05-08 16:35:00

标签: c++

我正在解决一个问题,陷入了课堂上的这个错误。

  

无效使用非静态数据成员'nr_piste'

     

bool HardDisk [nr_piste] [nr_sectoare];

     

无效使用非静态数据成员'nr_sectoare'

     

bool HardDisk [nr_piste] [nr_sectoare];

代码如下:

    class hard{
      public:
        int nr_piste, nr_sectoare, clusteri_ocupati;
        hard(){
            in >> nr_piste >> nr_sectoare >> clusteri_ocupati;
        }
        bool HardDisk[nr_piste][nr_sectoare];
        void insert(){
            int pista, sector;
            for (int i = 0; i < nr_piste; i++){
                for (int j = 0; j < nr_sectoare; j++){
                    HardDisk[i][j] = 0;
                }
            }
            for (int i = 0; i < nr_piste; i++){
                for (int j = 0; j < nr_sectoare; j++){
                    in >> pista >> sector;
                    HardDisk[pista][sector] = 1;
                }
            }
        }
    };

我尝试使用将变量私有化,但这没用。

1 个答案:

答案 0 :(得分:1)

您不能使用运行时变量来设置数组尺寸。

即使可以,数组(成员变量)也会在构造函数运行之前创建!

所以这一切不可能。

请尝试使用矢量,以便您可以根据需要调整其大小。