I have a program that is building a 3x3 matrix as a two dimensional array, and I am overloading the cout operator to display the matrix appropriately.
For example the identity matrix (default) should be displayed as:
1 0 0
0 1 0
0 0 1
However, there are build errors that I can't fix. Pleas help :(
int main()
{
//Multiply Identity matrix by scalar
Matrix3x3 m1;
cout << m1;
return 0;
}
////////////////////////////////////////////////////////////////////
//This is Matrix3x3.h
#ifndef MATRIX3X3_H
#define MATRIX3X3_H
#include <iostream>
#include <vector>
class Matrix3x3
{
public:
Matrix3x3(int a, int b, int c, int d, int e, int f, int g, int h, int i);
Matrix3x3();
int elements[3][3];
friend ostream& operator<<(ostream&, const Matrix3x3 &);
};
#endif
/////////////////////////////////////////////////////////////////////
//This is Matrix3x3.cpp
#include "Matrix3x3.h"
#include <iostream>
using namespace std;
Matrix3x3::Matrix3x3(int a, int b, int c, int d, int e, int f, int g, int h, int i)
{
elements[0][0] = a;
elements[0][1] = b;
elements[0][2] = c;
elements[1][0] = d;
elements[1][1] = e;
elements[1][2] = f;
elements[2][0] = g;
elements[2][1] = h;
elements[2][2] = i;
}
i am not 100% sure on this because I don't have the time at the moment to build your program butI am guessing it is because you friend class is suppose to be private (Not accessible in the public part of class. This is because it is an operator rather then a function you need to access. Also I think some compilers might not accept the function if there is a space between the operator and <<. I know I have ran into that problem in the past. Lastly, add the function:
1 2 3
Matric3x3::~Matrix3x3()
{
}
All classes should have that type of function in there to help it close or else you could potentially have problems, usually memory issues.
Take out the #include <iostream> in the cpp file because you already declared it in your header file. Also you declared vector but you don't use any vectors, that seems a waste. Lastly you don't need to declare <iostream> in main because it is already loaded into memory when you declared Matrix3x3.h.