gpt4 book ai didi

在这种结构下,c++ 在二维矩阵上使用 "indexing"

转载 作者:行者123 更新时间:2023-11-30 03:02:41 27 4
gpt4 key购买 nike

不知道为什么但我得到一个错误:在这个结构之后我不能索引矩阵,所以我不能在定义的矩阵上使用“索引方法”。谁能告诉我为什么?或者如何解决?

Header:
const int days=31;
const int exp=6;

struct Array{
int days;
int exp;
int **M;
};

构造函数:

void constr(Array loc){
//Construct of 31*6 Matrix, were 31 nr. of days and 6 specific types:
//0-HouseKeeping, 1-Food, 2-Transport, 3-Clothing, 4-TelNet, 5-others
loc.days = days;
loc.exp = exp;
loc.M = new int*[loc.days];
for(int i=0; i<loc.days;i++ ){
loc.M[i] = new int[loc.exp];
for (int j = 0; j< loc.exp; j++){
loc.M[i][j] = 0;
}
}
}

Controller .cpp

 void add(int cant,int tip, Array M){
//Adds to current day the amount to a specific type
currDay();
M[currentDay][tip] += cant; ////////////error
}


void insert(int zi,int tip,int cant, Array M){
//Adds to current day the amount to a specific type
M[zi][tip] = cant; ///////////error
}

void removeDay(int day, Array M){
for(int i = 0; i<6; i++)
M[day][i] = 0; ///////////error

//zi and tip ~ day type... for easier read.
//i need to manage the expenses of a family in a month doesn't matter which
ERROR: error: no match for 'operator[]'

UI(where constructor is used):

int main(){
Array M;
constr(M);
printMenu();
return 0;
}

最佳答案

您没有访问该成员:

M.M[currentDay][tip]

代替

M[currentDay][tip]

或者您可以为您的结构定义operator []:

struct Array{
int days;
int exp;
int **M;
int*& operator[] (int idx) { return M[idx]; }
};

关于在这种结构下,c++ 在二维矩阵上使用 "indexing",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9907248/

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