3

我对 C++ 还很陌生,并且我有一个项目,我需要在其中使用班级组织音乐会的座位表。麻烦的是,我在很多实现代码中都收到错误消息“错误:数组下标的无效类型'type [int]'”,即使我有理由确定我在类中的数组声明是合法的并且已实现正确。

这是包含该类的标头代码:

#ifndef HALL_H
#define HALL_H

#include <iostream>
#include <string>
using namespace std;

const double     PRICE_HI = 200.0;
    const int MIN_HI_SEAT = 5;
    const int MAX_HI_SEAT = 16;
    const char MAX_HI_ROW = 'H';
const double     PRICE_LO = 150.0;
const char       MAX_ROWS = 'N';
const int        NUM_ROWS = 14;
const int       NUM_SEATS = 20;
const int         MAX_RES = 12;
const double     DISCOUNT = 0.9;
const string        BLANK = "---";
const int        NUM_DAYS = 7;

class Hall {
    public:
        string  name[NUM_ROWS][NUM_SEATS];

        Hall         ();
        bool request (string, int, string, int);
        bool cancel  (string);
        void print   ();

    private:    
        char     row[NUM_ROWS];
        int     seat[NUM_SEATS];
        bool      hi[NUM_ROWS][NUM_SEATS];
        double price[NUM_ROWS][NUM_SEATS];

        bool process  (string, int, string, int);
        void revenue  ();
        string enough (int, string, bool);
        bool count    (int, bool);
        void make     (string, int, string, int, bool, string);
        void assign   (string, int, int, int, int, bool, bool, string);
        void leftover (string, int, int);
        void output   (string, int, int, int);
};

#endif

这是实现中的构造函数,作为获取错误消息的代码示例:

#include <iostream>
#include "hall.h"
using namespace std;

    Hall::Hall()
{   
    char row = 'A';
    int seat = 1;
    for (int i = 0; i < NUM_ROWS; i++) {
    for (int j = 0; j < NUM_SEATS; j++) {
        row[i]      = row;
        seat[j]     = seat;
        name[i][j]  = BLANK;
        price[i][j] = 0;
        if (row[i]  <= MAX_HI_ROW  &&
            seat[j] >= MIN_HI_SEAT &&
            seat[j] <= MAX_HI_SEAT) {
            hi[i][j] = true;
        } else {
            hi[i][j] = false;
        }
        seat++;
    }
    seat = 1;
    row++;
    }
}

以下是构造函数的错误消息:

hall.cpp: In constructor ‘Hall::Hall()’:                                                                                         
hall.cpp:11: error: invalid types ‘char[int]’ for array subscript
hall.cpp:12: error: invalid types ‘int[int]’ for array subscript
hall.cpp:15: error: invalid types ‘char[int]’ for array subscript
hall.cpp:16: error: invalid types ‘int[int]’ for array subscript
hall.cpp:17: error: invalid types ‘int[int]’ for array subscript

我已经对此感到困惑了一段时间,并感谢任何帮助。

干杯

4

3 回答 3

2

这些局部变量:

char row = 'A';
int seat = 1;

隐藏同名成员。要么选择不同的名称,要么以this->row和访问成员this->seat

于 2013-11-11T18:20:15.383 回答
0

将构造函数的局部变量命名为与类的数据成员相同的方式是一个非常糟糕的主意。那么这些陈述是什么意思呢?

row[i]      = row;
seat[j]     = seat;

至少写起来会更好

this->row[i]      = row;
this->seat[j]     = seat;

或者

Hall::row[i]      = row;
Hall::seat[j]     = seat;
于 2013-11-11T18:24:48.687 回答
0

您的变量名称重复,本地行变量覆盖您的行数组变量。

于 2013-11-11T18:20:15.353 回答