[英]incompatible assignment of type char to type char[9]
I can't for the life of me figure out why I'm getting this error message 我一生无法弄清楚为什么我收到此错误消息
incompatible types in assignment of `char' to `char[9]'
The code: 编码:
class CabinAssignment {
char room[3][4][9];
public:
CabinAssignment();
void display();
bool available(int floor, int row, int col);
void assignCabin(int floor, int row, int col);
};
CabinAssignment::CabinAssignment() {
for(int i = 0;i<3;i++) {
room[i][0] = 'A';
room[i][1] = 'B';
room[i][2] = 'C';
room[i][3] = 'D';
room[i][4] = 'E';
room[i][5] = 'F';
room[i][6] = 'G';
room[i][7] = 'H';
room[i][8] = 'I';
room[i][9] = 'J';
}
}
You declared room
as 3D array of char
elements. 您将
room
声明为char
元素的3D数组。 So, in order to access the actual char
s, you have to apply three indices to room
. 因此,为了访问实际的
char
,您必须将三个索引应用于room
。 Get it? 得到它? Three -dimensional array needs three indices for individual element access.
三个维数组需要各个元素访问三个指数。 For example,
room[i][j][k]
will give you access to the corresponding character. 例如,
room[i][j][k]
将使您可以访问相应的字符。
But if you apply only two indexes to room, as in room[i][4]
, you get 1D array as the result. 但是,如果仅对room应用两个索引,如
room[i][4]
,则将得到一维数组。 So in room[i][4] = 'E'
you are attempting to assign 'E'
to an 1D array of type char[9]
. 因此,在
room[i][4] = 'E'
您尝试将'E'
分配给char[9]
类型的一维数组。 That just does not make sense. 那只是没有意义。 That's your problem.
那是你的问题。
Why are you using only two indices? 为什么只使用两个索引? What was the point of declaring
room
as a 3D array? 将
room
声明为3D阵列有什么意义? What were you trying to do? 你想做什么?
Also the second size of your array is only 4
, which means that the second index must lie between 0
and 3
. 同样,数组的第二个大小只有
4
,这意味着第二个索引必须位于0
到3
之间。 Yet you are attempting to access it out of bounds, as in room[i][9]
. 但是,您正在尝试无限制地访问它,例如在
room[i][9]
。
char room[3][4][9];
says that room is an array of 3 x 说房间是3 x的阵列
char[4][9];
which is an array of 4 x 这是一个4 x的数组
char[9];
which is an array of 9 chars. 这是9个字符的数组。
room[i][0] = 'A';
Is trying to assign a single char to what is actually an array of char[9]; 试图将单个char分配给实际上是char [9]的数组;
You can use typedef to make this easier on yourself. 您可以使用typedef使自己更轻松。
typedef char COLUMN;
typedef COLUMN ROW [9]; // A row has 9 columns
typedef ROW FLOOR [4]; // A floor has 4 rows.
char FLOOR room[3]; // this object has 3 floors of 4 rows or 9 columns
Or better still, you could use inline-classing. 或者更好的是,您可以使用内联分类。
#include <array> // this lets you do std::array<type, size> but requires c++11
class CabinAssignment {
public:
class Column {
char m_value;
public:
Column(char value_ = 0) : m_value(value_) {}
char get() const { return m_value; }
void set(char value_) { m_value = value_; }
};
class Row {
std::array<Column, 9> m_columns;
public:
Row() : m_columns() {}
const Column& col(size_t colNo_) const { return m_columns[colNo_]; }
Column& col(size_t colNo_) { return m_columns[colNo_]; }
};
class Floor {
std::array<Row, 4> m_rows;
public:
Floor() : m_rows() {}
const Row& row(size_t rowNo_) const { return m_rows[rowNo_]; }
Row& row(size_t rowNo_) { return m_rows[rowNo_]; }
};
private:
std::array<Floor, 3> m_floors;
public:
CabinAssignment() : m_floors() {}
const Floor& floor(size_t floorNo_) const { return m_floors[floorNo_]; }
Floor& floor(size_t floorNo_) { return m_floors[floorNo_]; }
};
int main() {
CabinAssignment cab;
cab.floor(1).row(3).col(8) = 'A';
char whoseOnFloor2Row5Col1 = cab.floor(2).row(5).col(1).get();
}
There are various ways you could eliminate the "get()" at the end, I was trying to go with a single theme. 最后,有多种方法可以消除“ get()”,我试图使用单个主题。 Charles Bailey points out you could make Column a simpler struct
查尔斯·贝利(Charles Bailey)指出,您可以使Column成为一个更简单的结构
struct Column { char value; }
And then 接着
char whoseOnFloor2Row5Col1 = cab.floor(2).row(5).col(1).value;
I've gotten into the habbit of prefixing my member variables with "m_", though, which would make it 我已经习惯了在成员变量前加上“ m_”作为前缀,
struct Column { char m_value; }
char whoseOnFloor2Row5Col1 = cab.floor(2).row(5).col(1).m_value;
and I balked at having an m_ exposed like that, but there's no real reason not to. 我拒绝让m_这样暴露,但没有真正的理由不这样做。
If you're not able to use std::array, simply change the definition of, eg m_columns to 如果您无法使用std :: array,只需将例如m_columns的定义更改为
Column m_columns[9];
and so on for m_rows and m_floors, but then you will probably want to do index checks on floor(), row() and column(). 以此类推,对于m_rows和m_floors,等等,但是您可能要对floor(),row()和column()进行索引检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.