I need to print static matrix overloading "<<" operator. Here is my code:
class Matrix
{
public:
int matrix[3][3];
friend std::ostream& operator<<(std::ostream& out, const Matrix& e);
};
std::ostream& operator<<(std::ostream& out, const Matrix& e)
{
for (int i = 0; i < 3; i )
{
for (int j = 0; j < 3; j )
{
out << e.matrix[i][j]<<" ";
}
out << std::endl;
}
out << std::endl;
return out;
}
and main function
int main
{
int A[3][3] = { {1,1,1},{1,0,0},{0,0,1} };
}
My problem is I don't know how to use in main function my overloaded operator to print matrix A.
CodePudding user response:
I don't know how to use in main function my overloaded operator to print matrix A
First, you need to create an instance of your Matrix
class, then you can print it:
int main()
{
Matrix A = {{{1,1,1}, {1,0,0}, {0,0,1}}};
std::cout << A;
}
Side note: I suggest replacing all out << std::endl;
with out << '\n';
in your operator<<
. You should let the user std::flush
if the user actually needs it.