It tells you not to try to make it work. It tells you to use array(i,j).
With brute force, you can make the subscript variant work, of course. You just need to have operator[] return an object that also provides operator[] itself.
@Athar "You just need to have operator[] return an object that also provides operator[] itself." I dont know how to do this. I read this forum page http://v2.cplusplus.com/forum/general/8017/ but the problem is I can only have one main class.
Sorry I am new to this
I just meant that I have main.ccp and test.h /.cpp with a private template multidimensional array and I have to be able to access it using [][] in my main.cpp. I have overloaded all the other operation such as *,+= but have no idea how to overload [][]. I know how to get the same result using operator()
EDIT:
to clarify... [][] is not an operator. [] is the operator.
/EDIT
Not to sound like a broken record... but the point is that you shouldn't overload [][] because it's problematic. It splits the job of one task (dereferencing) across two separate calls (one call for each [] operator).
If you really want to make this work, you need to overload the [] operator so that it returns a type that can also have the [] operator used on it. This means:
1) return a pointer (awful because it breaks encapsulation and prevents the possibility of bound checking)
or
2) Creating a sub-class that also has the [] operator overloaded. (awful because it's excessively complicated and usually requires some interclass dependencies).
The idea is this:
1 2 3 4 5 6 7 8
mymatrixtype<int> foo;
// this:
foo[row][column] = 5;
// is really this:
mysubtype<int>& bar = foo[ row ]; // foo[] returns a subtype which gives you the row
bar[ column ] = 5; // bar[] returns the actual data
template<class Y>
class test{
public:
Y * operator[](int rows){
return (array[rows]);
}
private:
Y** array;
int ro;
int col;
};
This is what I have but I don't know how to implement the solution 1 suggest above. I have to be able to use [][] in my int main(). Sorry that I am not understanding this