1

don't know why but i get an error: after this structure i can't index the matrix, so i cant use the "indexing method" over the defined matrix.Can anyone tell me why? or how to fix it?

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

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

Constuctor:

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;
}
Bogdan Maier
  • 663
  • 2
  • 13
  • 19
  • Your example is not minimal (the `constr` function is not used) and it could be reduced into a single file. Could you please reduce it and give the line of the error? – thiton Mar 28 '12 at 12:14
  • The `constr` function is allocating a copy of the original object. So this will cause memory leaks at exit and original object won't be allocated. It works in the 3 others function because pointer is copied too (if it has been allocated) – Patrice Bernassola Mar 28 '12 at 12:26
  • 1
    I need to wash my eyes, is this question really tagged `C++`? – Andre Mar 28 '12 at 12:31

3 Answers3

2

You're not accessing the member:

M.M[currentDay][tip]

instead of

M[currentDay][tip]

Or you could define operator [] for your struct:

struct Array{
    int days;
    int exp;
    int **M;
    int*& operator[] (int idx) { return M[idx]; }
};
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

You are trying to call operator[] on the type array. You need to get the pointer member first.

M.M[day][i];

That said: You are not writing C++ but some obscure form of bad C. You might want to have a look at the book list and read one of them before pursuing coding any further.

Community
  • 1
  • 1
pmr
  • 58,701
  • 10
  • 113
  • 156
0

You have at least two problems:

  1. The first is that you are using the actual structure as the array, which wont work. Use e.g. M.M[day][i].
  2. The second is that when you create the array, you pass the structure by value, this means it will be copied to a local variable in the constr function and the data will not be available in the function calling constr. Pass it as a reference instead, i.e. void constr(Array &loc).

The second problem can also be solved by using a constructor in the structure, instead of a separate function:

const int DAYS=31;
const int EXP=6;

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

    // Constructor, called when an instance of structure/class is created
    Array(){
        days = DAYS;
        exp = EXP;
        M = new int*[days];
        for(int i=0; i<days;i++ ){
            M[i] = new int[exp];
            for (int j = 0; j< exp; j++){
                M[i][j] = 0;
            }
        }
    }

    // Destructor, called when structure/class is destroyed
    ~Array(){
        if(M){
            for(int i=0;i<days;i++){
                if(M[i])
                    delete [] M[i];
            }
            delete [] M
        }
    }

    // Copy constructor, called when instance of structure/class is copied
    Array(const Array &array){
        days = array.days;
        exp = array.exp;
        M = new int*[days];
        for(int i=0; i<days;i++ ){
            M[i] = new int[exp];
            for (int j = 0; j< exp; j++){
                M[i][j] = array.M[i][j];
            }
        }
    }
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621