gardian06
gardian06

Reputation: 1546

accessing multi-dimensional array element c++

I am trying to work with a multi-dimensional array in MSVS2010 console application, and I need to access members of a 2D array. I instantiate the array as

Thing::Thing(int _n){
    // size of the array
    this.m = _n;
    thing = new int*[m];
    for(int ii = 0; ii < m; ii++){
        thing[ii] = new int[m];
    }
}

this is working fine. though when I go to do a operator=, or operator== that both use the similar structure of:

Thing& Thing::operator=(const Thing & _thing){
    for(int ii = 0; ii < m; ii++){
        for(int jj = 0; jj < m; jj++){
            thing[ii][jj] = _thing[ii][jj]; //error thrown on this line
        }
    }
    return *this;
}

this throws 2 errors

binary "[": 'const Thing' does not define this operator or a conversion to a type acceptable to the predefined operator
IntelliSense: no operator"[]" matches these operands

this doesn't make sense as it is an array of type int, and the "[]" operators have not been altered not to mention that error highlighting only puts it under:

_thing[ii][jj];

I can kinda live without the assignment operator, but I need the comparison operator to have functionality.

Upvotes: 3

Views: 748

Answers (3)

6502
6502

Reputation: 114599

Thing is the class, thing is the member, thing the parameter... and you forgot that if you want to access the member in the operator= call then you should use _thing.thing.

Your naming choice is quite bad, so bad that it even confused yourself while you were writing the code (and if it was easy for you to make a mistake now try to imagine how much easier would be for someone else to get confused by this code or even for you in a few months from now).

What about calling for example the class Array instead, the member data and the parameter other? I also would suggest avoiding having leading underscores in names, they are ugly and dangerous at the same time (do you know all the C++ rules about where you can put underscores in names and how many of them you are allowed to use?).

When designing a class or a function you have many things to consider and the class name or the function name is important but is one of the many factors. But for a data member or a variable you only have to choose the type and the name and both of them are most important choices.

So please take the habit of thinking carefully to names, especially of variables. The relative importance is tremendous for them. Variables and data members are just names... the name is actually the only reason for which in programming we like to use variables (the computer instead only uses numeric addresses and is perfectly happy with them).

About the class design you probably would also like defining operator[](int)...

int *operator[](int index) { return data[index]; }

By doing this you will be able to write code like

Array a(m);
a[0][0] = 42;

without the need to explicitly refer to data (and, by the way, this addition would also make your original code working... but still fix the names!!).

Upvotes: 2

sirgeorge
sirgeorge

Reputation: 6541

You should do: thing[ii][jj] = _thing.thing[ii][jj]; in your assignment loop. And you should also check if the array sizes for both (this and _thing) are the same: it may give a crash otherwise.

You get an error because you are trying to use operator[] (indexing operator) on an object class Thing, not on its internal array. If you want to use the Thing class like an array you should define an indexing operator for it e.g.:

int* Thing::operator[](int idx)
{
   return thing[idx];
}

Upvotes: 3

naumcho
naumcho

Reputation: 19941

I think you've got your "thing"-s confused. Since:

   Thing& Thing::operator=(const Thing & _thing)

you probably want to have:

   thing[ii][jj] = _thing.thing[ii][jj];

_thing is the Thing object

_thing.thing is the multidimensional array

Upvotes: 2

Related Questions