gpt4 book ai didi

c++ - 类成员数组 : invalid type for array subscript 的实现

转载 作者:太空狗 更新时间:2023-10-29 21:44:30 25 4
gpt4 key购买 nike

我是 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

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

干杯

最佳答案

这些局部变量:

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

隐藏同名成员。选择不同的名称,或以 this->rowthis->seat 访问成员。

关于c++ - 类成员数组 : invalid type for array subscript 的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19913068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com