Reputation: 592
This is a program that I've written to create a Matrix class from a vector, with the intent to overload operators. There may be better ways of doing this, but at this point I'm just having one problem. For some reason in my overloaded operator+ function, it does the function correctly, however, when it returns the Matrix to main(), everything becomes lost.
In main, i do test3 = test + test2. The +operator works (I've tested). The =operator works(I've tested with just test3 = test), but when I combine them like this it doesn't work.
#include <iostream>
#include <vector>
using namespace std;
Class defintion
class Matrix
{
private:
vector<vector<int>> Mat;
Matrix() {};
public:
Matrix(vector<vector<int>> mat): Mat(mat) {cout << "Constructor" << endl;};
~Matrix() {};
Matrix(const Matrix &rhs);
//mutators & accessors
int getnum(int r, int c)const {return Mat[r][c];};
int getrow()const {return (Mat.size());};
int getcol()const {return (Mat[0].size());};
//overloaded operators
friend ostream & operator<<(ostream &os, const Matrix &rhs);
Matrix &operator+(const Matrix &rhs);
Matrix &operator=(const Matrix &rhs);
//Matrix &operator*(const Matrix &lhs, const Matrix &rhs);
//Matrix &operator*(const Matrix &lhs, const vector<int> &rhs);
};
Main
int main()
{
vector<vector<int>> A(4,vector<int>(4,0));
vector<vector<int>> B(4,vector<int>(4,0));
vector<vector<int>> C(1,vector<int>(1,0));
for(int i(0); i < 4; ++i)
for(int j(0); j < 4; ++j)
{
A[i][j] = i;
B[j][i] = i;
}
Matrix test(A);
Matrix test2(B);
Matrix test3(C);
test3 = test + test2;
cout << test3;
return 0;
}
Copy Constructor
Matrix::Matrix(const Matrix &rhs)
{
Mat = rhs.Mat;
cout << "copy constructor" << endl;
}
Overloaded Operators
ostream &operator<<(ostream &os, const Matrix &rhs)
{
for(int i(0); i < rhs.getrow(); ++i)
{
for(int j(0); j < rhs.getcol(); ++j)
os << rhs.getnum(i, j) << '\t';
os << endl;
}
return os;
}
Matrix &Matrix::operator+(const Matrix &rhs)
{
vector<vector<int>> temp(getrow(), vector<int>(getcol(), 0));
Matrix Temp(temp);
if((getrow() != rhs.getrow()) || (getcol() != rhs.getcol()))
{
cout << "Addition cannot be done on these matrices (dims not equal)" << endl;
exit(1);
}
for(unsigned int i(0); i < Temp.getrow(); i++)
for(unsigned int j(0); j < Temp.getcol(); j++)
Temp.Mat[i][j] = getnum(i, j) + rhs.getnum(i, j);
return Temp;
}
Matrix &Matrix::operator=(const Matrix &rhs)
{
cout << rhs.getrow() << endl;
this->Mat.resize(rhs.getrow());
for(int i(0); i < this->getrow(); ++i)
this->Mat[i].resize(rhs.getcol());
for(int i(0); i < this->getrow(); ++i)
for(int j(0); j < this->getcol(); ++j)
this->Mat[i][j] = rhs.Mat[i][j];
return *this;
}
Upvotes: 1
Views: 5873
Reputation: 526
The problem with your code is when you do this test3 = test + test2;
The overloaded operator + returns a reference to a local variable Temp
.
You can't return a reference to a local variable, because t will get destroyed as soon as the function exits. So, inside the operator+() function, you can't return a reference to that variable, because it no longer exists when the calling function gets the return value.
You can change your definition to
Matrix Matrix::operator+(const Matrix &rhs)
and it will compile and work fine.
Upvotes: 1