I have trouble understanding the problem in the title. So there are my two classes, a Vector of 3 doubles and a 2D dynamic Matrix that has a Vector object in every cell. The constructor of Matrix works and does not throw errors. However, when I want to refer to a cell of created Matrix instance in the ostream overload, I'm getting
"no match for 'operator[]' (operand types are 'Matrix' and 'int')"
Why is it OK to use the [][] notation during initialization and not OK later? Is there a moderately straightforward way to fix this? Many thanks!
class Vector{
private:
double x, y, z;
public:
Vector(){
x = y = z = 0;
}
Vector(int x_, int y_, int z_){
x = x_;
y = y_;
z = z_;
}
friend ostream &operator<< (ostream &wyj, Vector &v);
friend istream &operator>> (istream &wej, Vector &v);
};
/// ===== MATRIX CLASS CONSISTS OF VECTOR OBJECTS
class Matrix{
private:
Vector ** M;
int row;
int col;
public:
Matrix(int col, int row){
M = new Vector * [row];
for(int i = 0; i < row; i++){
M[i] = new Vector[col];
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
M[i][j] = Vector();
}
}
}
friend ostream &operator<< (ostream &wyj, Matrix &M);
};
ostream &operator<< (ostream &wyj, Matrix &M){
for(int i = 0; i < M.row; i++){
for(int j = 0; j < M.col; j++){
wyj << M[i][j] << " ";
}
wyj<< endl;
}
return wyj;
}
int main(){
Matrix A(2, 2);
cout << A[1][1]; // LURD VADAR SAYZ NOOOOOOOOOOOOOOO
}
EDIT: minor typos in << overload method