So I have the += operator overloaded in a way that attempts to take advantage of the + operator that I've already defined. Namely that Polynomial + Polynomial returns a new Polynomial. So my += function basically tries to call this + function with the LHS as *this and the RHS as *this + B (where B is a Polynomial object passed by const reference to the function). I am getting an error. Where am I going wrong with this?
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
void line(int lines);
class Polynomial
{
private:
int degree;
double* coeffs;
public:
//constructors
Polynomial() {degree=0;coeffs=new double[1];}
Polynomial(int deg) {degree=deg;coeffs=new double[deg+1];}
Polynomial(const Polynomial& A);
//mutators
void GetCoeffs(istream& in);
void EditCoeff(int deg);
void ResetCoeffs();
int Coeff(int deg);
void Randomize(int max);
//accessors
void Show(ostream& out);
int Degree() {return degree;}
//operators
Polynomial operator+(const Polynomial& B); //Polynomial + Polynomial
friend Polynomial operator +(double c, Polynomial& A); //c + Polynomial
Polynomial operator +(double c); //Polynomial + c
void operator +=(const Polynomial& B); //Polynomial+=Polynomial
void operator =(Polynomial& A);
Polynomial operator*(int k);
Polynomial operator*(Polynomial& A);
};
int main()
{
Polynomial A(5);
A.Randomize(4);
A.Show(cout);
line(2);
Polynomial B=A+8;
B.Show(cout);
return 0;
}
Polynomial Polynomial::operator*(int k)
{
Polynomial C(degree);
C=*this;
for (int i=degree; i>=0; i--)
C.coeffs[i]*=k;
return C;
}
Polynomial operator +(double c, Polynomial& A)
{
Polynomial C=A;
C.coeffs[0]+=c;
return C;
}
Polynomial Polynomial::operator +(double c)
{
Polynomial C=*this;
C.coeffs[0]+=c;
return C;
}
void Polynomial::Randomize(int max)
{
for (int i=degree; i>=0; i--)
{
coeffs[i]=rand()%(max+1) + 1;
if ((rand()%(101) + 1)%2 == 0)
coeffs[i]*=-1;
}
}
void Polynomial::operator =(Polynomial& A)
{
if (degree==A.degree)
{
for (int i=degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];
}
}
}
Polynomial Polynomial::operator+(const Polynomial& B)
{
if (degree>=B.degree)
{
Polynomial C(degree);
C=*this;
for (int i=B.degree; i>=0; i--)
{
C.coeffs[i]=coeffs[i]+B.coeffs[i];
}
return C;
}
else
{
Polynomial C=B;
for (int i=degree; i>=0; i--)
{
C.coeffs[i]=coeffs[i]+B.coeffs[i];
}
return C;
}
}
void Polynomial::operator+=(const Polynomial& B)
{
*this = (*this + B);
}
int Polynomial::Coeff(int deg)
{
return coeffs[deg];
}
void line(int lines)
{
for (int i=0; i<lines; i++)
cout << endl;
}
void Polynomial::GetCoeffs(istream& in)
{
for (int i=degree; i>=0; i--)
{
in >> coeffs[i];
}
in.ignore();
}
void Polynomial::Show(ostream& out)
{
if (coeffs[degree]>0)
cout << " ";
for (int i=degree; i>=0; i--)
{
if (coeffs[i]>=0)
{
if (i!=degree)
out << " + ";
out << coeffs[i];
}
else
{
if (coeffs[i]<0)
out << " - ";
out << 0-coeffs[i];
}
if (i>1)
out << "x^" << i;
else if (i==1)
out << "x";
}
}
Polynomial::Polynomial(const Polynomial& A)
{
coeffs=new double[A.degree+1];
degree=A.degree;
for (int i=A.degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];
}
}
Polynomial Polynomial::operator*(Polynomial& A)
{
int deg=A.degree+degree;
Polynomial P(deg);
for (int i=deg; i>=0; i--)
P.coeffs[i]=0;
for (int i=deg; i>=0; i--)
{
for (int j=A.degree; j>=0; j--)
{
P.coeffs[i+j]+=coeffs[i]*A.coeffs[j];
}
}
return P;
}
Your problem is that *this + B is a temporary, and temporary objects can't be bound to non-const references.
Of course, there's no reason that the RHS of an assignment can't be const. Try:
void operator=(/* HERE */ const Polynomial& A);
Most of your other operators aren't using const where they should either. For example:
Polynomial operator+(const Polynomial& B) /* HERE */ const; //Polynomial + Polynomial
friend Polynomial operator +(double c, /* HERE */ const Polynomial& A); //c + Polynomial
Only assignment operators should be non-const members, and they should accept const right operands. Normal binary operators that create new objects should be const wrt both left and right operands.
Often it is done other way around. People make binary + operator (usually non-member) that takes advantage of += operator (a member) that is already defined.
Where am I going wrong with this?
You should use std::vector instead of a raw pointer.
You have violated the rule of three.
You didn't follow the convention creating a member operator+= and a free operator+.
Try this:
// UNTESTED
class Polynomial
{
private:
std::vector<double> coeffs;
public:
//constructors
Polynomial() : coeffs(1) {}
Polynomial(int deg) : coeffs(deg+1) {}
// Don't need copy constructor
// Don't need destructor
// Don't need assignment operator
...
int Degree() {return coeffs.size()-1;}
//operators
Polynomial& operator+=(const Polynomial& B) {
if(Degree() < B.Degree())
coeffs.resize(B.Degree()+1);
for(int i = 0; i <= B.Degree(); ++i)
coeffs[i] += B.coeffs[i];
return *this;
}
};
Polynomial operator+(Polynomial A, const Polynomial& B) {
return A += B;
}
Related
I'm learning C++ and trying to write a C++ class for matrices, where I store the matrix as a one-dimensional C array. To this end, I defined an element member function to access the matrix elements based on their location in the array. I have then overloaded the << and + operators to handle the display and addition of matrices. The << operator works as intended, as can be demonstrated in the following example:
#include<iostream>
class matrix {
friend std::ostream & operator<<(std::ostream &os, matrix &M);
private:
int rows{}, columns{};
double *array {nullptr};
public:
matrix(int rows, int columns);
~matrix() {}
double & element(int r, int c);
matrix operator+(matrix &M)
{
matrix N(rows, columns);
if (M.rows!=rows || M.columns!=columns){
std::cout << "ERROR: DIMENSIONS OF MATRICES DO NOT MATCH" << std::endl;
}
else{
for (int i{1}; i<=M.rows; i++)
for (int j{1}; j<=M.columns; j++){
N.element(i,j) = element(i,j) + M.element(i,j);
}
}
return N;
}
};
matrix::matrix(int r, int c)
{
rows = r;
columns = c;
array = new double[rows*columns];
for(int i{0}; i<rows*columns; i++) array[i]=0.0;
}
double & matrix::element(int i, int j)
{
int loc{0};
loc = (j-1) + (i-1)*columns;
return array[loc];
}
std::ostream & operator<<(std::ostream &os, matrix &M)
{
for (int i{1}; i<=M.rows; i++){
os << "[ ";
for (int j{1}; j<=M.columns; j++){
os << M.element(i,j) << " ";
}
os << "] \n";
}
return os;
}
int main() {
matrix A(2,2);
matrix B(2,2);
A.element(1,1) = 1;
A.element(1,2) = 2;
A.element(2,1) = 3;
A.element(2,2) = 4;
B.element(1,1) = 1;
B.element(1,2) = 2;
B.element(2,1) = 3;
B.element(2,2) = 4;
std::cout << A << std::endl;
std::cout << B << std::endl;
return 0;
}
I'm then unable to display the addition of two matrices:
std::cout << A+B << std::endl;
If I, however, break up the operation and add the matrices separately before displaying their sum, I get the right output, which makes me believe that the + operator also works correctly:
matrix C(2,2);
C = A+B;
std::cout << C << std::endl;
The error seems to suggest that there may be an issue converting the matrix elements into the ostream, but it's curious that the above workaround solution works.
Your << operator takes a non-const reference to a matrix as parameter. That means it can't take reference to a temporary object.
The result of A+B is a temporary object if you don't assign it to something.
So you need to change this:
std::ostream & operator<<(std::ostream &os, matrix &M);
into this:
std::ostream & operator<<(std::ostream &os, const matrix &M);
You may sooner or later encounter the same problem with your +-operator:
matrix operator+(matrix &M)
should be
// Both `M` and `*this` should be const
matrix operator+(const matrix &M) const
Naturally you will then have a problem with your element method which can only act on a non-const object, so you also need a variant that acts on a const object:
class matrix
{
....
public:
double & element(int r, int c);
double element(int r, int c) const;
...
}
You can't take a non-const reference of a temporary object. You're trying to call operator<< with a temporary (A+B) but the signature of your overloaded operator<< takes a non-const reference so your function isn't a valid candidate.
To fix this you'll need to change a couple things. First, make your overloaded << operator take a const reference to the matrix. And don't forget to fix the friend signature in the class declaration.
std::ostream & operator<<(std::ostream &os, const matrix &M);
Just doing that will still cause errors because the element method is only defined for non-const objects. You'll need to add an overload for element that acts on const matrixes (and that won't let you modify the returned element).
double matrix::element(int i, int j) const
{
int loc = (j-1) + (i-1)*columns;
return array[loc];
}
Your operator<< and operator+ both need to take the input matrix by const reference, so that they can accept temporary matrix objects as input.
Also, operator+ should be const-qualified since it doesn't modify the contents of this. And element() should have a const -qualified overload so it can provide read-only access to a const matrix object.
You are also missing a copy constructor, a copy-assignment operator, a move constructor, and a move-assignment operator, per the Rule of 3/5/0. And also, your destructor is not freeing the C array at all, so it is being leaked.
Try this instead:
#include <iostream>
#include <stdexcept>
#include <utility>
class matrix {
friend std::ostream& operator<<(std::ostream &os, const matrix &M);
private:
int rows{}, columns{};
double* array{nullptr};
public:
matrix(int rows, int columns);
matrix(const matrix &M);
matrix(matrix &&M);
~matrix();
matrix& operator=(matrix M);
double& element(int r, int c);
double element(int r, int c) const;
matrix operator+(const matrix &M) const;
matrix& operator+=(const matrix &M);
};
matrix::matrix(int r, int c)
: rows(r), columns(c)
{
size_t size = rows*columns;
array = new double[size];
for(size_t i = 0; i < size; ++i)
array[i] = 0.0;
}
matrix::matrix(const matrix &M)
: rows(M.rows), columns(M.columns)
{
size_t size = rows*columns;
array = new double[size];
for(size_t i = 0; i < size; ++i)
array[i] = M.array[i];
}
matrix::matrix(matrix &&M)
: rows(M.rows), columns(M.columns), array(M.array)
{
M.rows = M.columns = 0;
M.array = nullptr;
}
matrix::~matrix()
{
delete[] array;
}
matrix& operator=(matrix M)
{
std::swap(rows, M.rows);
std::swap(columns, M.columns);
std::swap(array, M.array);
return *this;
}
double& matrix::element(int r, int c)
{
// TODO: do bounds checking here...
size_t loc = ((r-1)*columns) + (c-1);
return array[loc];
}
double matrix::element(int r, int c) const
{
// TODO: do bounds checking here...
size_t loc = ((r-1)*columns) + (c-1);
return array[loc];
}
std::ostream & operator<<(std::ostream &os, const matrix &M)
{
for (int r = 1; r <= M.rows; ++r){
os << "[ ";
for (int c = 1; c <= M.columns; ++c){
os << M.element(r,c) << " ";
}
os << "]\n";
}
/* alternatively...
size_t loc = 0;
for (int r = 0; r < M.rows; ++r){
os << "[ ";
for (int c = 0; c < M.columns; ++c){
os << M.array[loc++] << " ";
}
os << "]\n";
}
*/
return os;
}
matrix matrix::operator+(const matrix &M) const
{
matrix N(*this);
N += M;
return N;
}
matrix& matrix::operator+=(const matrix &M)
{
if (M.rows != rows || M.columns != columns){
throw std::runtime_error("DIMENSIONS OF MATRICES DO NOT MATCH");
}
for (int r = 1; r <= rows; ++r)
for (int c = 1; c <= columns; ++c){
element(r,c) += M.element(r,c);
}
}
/* alternatively:
size_t size = rows*columns;
for (size_t i = 0; i < size; ++i)
array[i] += M.array[i];
}
*/
return *this;
}
int main() {
matrix A(2,2);
matrix B(2,2);
A.element(1,1) = 1;
A.element(1,2) = 2;
A.element(2,1) = 3;
A.element(2,2) = 4;
B.element(1,1) = 1;
B.element(1,2) = 2;
B.element(2,1) = 3;
B.element(2,2) = 4;
std::cout << A << std::endl;
std::cout << B << std::endl;
return 0;
}
I have the following code for defining a class Matrix. The definitions in the header file are as follows:
#ifndef MATRIX_H
#define MATRIX_H
/* General matrix class */
class Matrix
{
public:
Matrix(int m, int n, double ini);
virtual ~Matrix();
Matrix(const Matrix& other); //copy ctor
Matrix& operator=(const Matrix& other); //assignment operator;
double operator()(int i, int j) const; //access element in the matrix
double& operator() (int i, int j); //set element in the matrix
friend Matrix operator+(const Matrix& mat1, const Matrix& mat2); //Matrix addition
friend Matrix operator+(const Matrix& mat1, double a); //scaler multiplication
int dimension() const {return rows*cols;} //getter method for dimension
protected:
private:
int rows; //number of rows in the matrix
int cols; //number of cols in the matrix
double* d; //pointer to the representation of the matrix
};
The implementation of the parts relevant to the question are shown below.
#include "Matrix.h"
#include<iostream>
Matrix::Matrix(int m, int n, double ini):rows{m},cols{n},d{new double[m*n]}
{
//ctor
double* p = d;
for(int i=0;i<rows*cols;i++)
{
*p++ = ini;
}
}
Matrix::~Matrix()
{
//dtor
delete []d;
}
Matrix& Matrix::operator=(const Matrix& rhs)
{
if (this == &rhs) return *this; // handle self assignment
if (rows*cols<=rhs.rows*rhs.cols)
{
delete []d;
d = new double[rhs.rows*rhs.cols];
rows = rhs.rows;
cols = rhs.cols;
for(int i=0;i<rows*cols;i++)
{
d[i] = rhs.d[i];
}
}
//assignment operator
return *this;
}
double Matrix::operator()(int i, int j) const
{
return d[rows*i + j];
}
double& Matrix::operator()(int i, int j)
{
return d[rows*i+j];
}
Now, I have a simple test application that creates a matrix, assigns values to the elements in the matrix, as well reads the value of an element (given a row and a column number).
#include <iostream>
#include "Matrix.h"
using namespace std;
int main()
{
int i,j;
Matrix A(3,2,0.0);
cout<<A.dimension()<<endl;
// assign values to matrix elements
for (i=0;i<3;i++)
{
for (j=0;j<2;j++) A(i,j) = 0.1*i*j;
}
// access matrix elements
double sum = 0.0;
for (i=0;i<3;i++) {
for (j=0;j<2;j++) sum += A(i,j); }
cout << "The sum of the matrix elements is ";
cout << sum << endl;
return 0;
}
My issue is that while everything compiles without problems, when run, the main function freezes -- the "sum" above is computed though. Was wondering if this is due to the destructor not being called or being unable to be called. Much appreciated if anyone has any ideas.
I think your code are wrong in operator()
double Matrix::operator()(int i, int j) const
{
return d[cols*i + j];
}
double& Matrix::operator()(int i, int j)
{
return d[cols*i+j];
}
And you overflow the array d[]
I am trying to create my own vector class. I want to overload the basic features of STL vector. My code is compiling by after testing it, a message "segmentation fault(core dumped is)" is shown. Could you show me where I can improve my code. Thank you in advance!
#include<iostream>
#include<fstream>
using namespace std;
class Vector
{
public:
Vector();
Vector(const Vector& vect);
~Vector();
Vector& operator =(const Vector& vect);
void insert(double element);
bool operator ==(const Vector& vect);
Vector& operator +(const Vector& vect);
Vector& operator -(const Vector& vect);
Vector& operator *(const Vector& vect);
int size();
double operator[](int num);
friend istream& operator >>(istream& input, Vector& vect);
friend ostream& operator <<(ostream& output, Vector& vect);
private:
double* data;
int num;
int max;
};
Vector:: Vector()
{
double* data = NULL;
int num = 0;
int max = 0;
}
Vector::Vector(const Vector& vect)// not sure if its correct
{
data = new double[vect.max];
memcpy(data, vect.data, sizeof(double) * vect.num);
num = vect.num;
max = vect.max;
}
Vector::~Vector()
{
if(data)
{
delete[] data;
data = 0;
}
}
void Vector:: insert(double element)
{
if(num < max)
{
*(data + num) = element;
}
else
{
double *tmp = new double[num * 2];
delete[] data;
*(data + num) = element;
max *= 2;
}
num++;
}
Vector& Vector:: operator = (const Vector& vect)// highly unlikely to be correct
{
if(this != &vect)
{
if(data)
{
delete[] data;
data = 0;
}
max = vect.max;
data = new double[max];
for(int i = 0; i < num; i++)
{
this->data[i] = vect.data[i];
}
}
return *this;
}
bool Vector:: operator ==(const Vector& vect)
{
bool isEqual = true;
if(this->num != vect.num)
{
isEqual = false;
}
for(int i = 0; i < num; i++)
{
if(this->data[i] != vect.data[i] )
{
isEqual = false;
}
}
return isEqual;
}
Vector& Vector:: operator +(const Vector& vect)
{
Vector result;
for (int i = 0; i < num; ++i)
{
result.insert(*(data + i) + *(vect.data + i));
}
return result;
}
Vector& Vector:: operator -(const Vector& vect)
{
Vector result;
for (int i = 0; i < num; ++i)
{
result.insert(*(data + i) - *(vect.data + i));
}
return result;
}
Vector& Vector::operator*(const Vector &vect)
{
Vector result;
for (int i = 0; i < num; ++i)
{
result.insert(*(data + i) * *(vect.data + i));
}
return result;
}
int Vector:: size()
{
return num;
}
double Vector::operator[](int num)
{
return *(data + num);
}
ostream &operator <<(ostream &output, Vector &vect)
{
output << "[";
for (int i = 0; i < vect.size(); ++i)
{
output << vect[i];
if (i < vect.size() - 1)
output << ",";
}
output << "]\n";
return output;
}
istream &operator >>(istream &input, Vector &vect)
{
double element;
input >> element;
vect.insert(element);
return input;
}
int main()
{
Vector vec;
for (int i = 0; i < 10; ++i)
{
vec.insert(i);
}
return 0;
}
Look closely at your insert function. Specifically at how you are re-sizing the array.
Here is my code, why is it showing segmentation fault? Can someone correct my mult4 function... I know that problem is mult4 and i am not sure is function mult4 is correct solution for polynomial recursively multiplication...
#include <iostream>
#include <cstdlib>
using namespace std;
class Polynomial
{
public:
Polynomial()
{
deg=0;
coeff = new float[deg+1];
coeff[0]=0;
}
Polynomial(int n)
{
deg = n;
coeff = new float[deg+1];
for(int i = 0; i<=deg;i++)
coeff[i] = rand()/(float)RAND_MAX;
}
Polynomial(float *array, int n)
{
deg=n;
coeff=array;
}
Polynomial(const Polynomial& P)
{
deg = P.deg;
coeff = new float[deg+1];
for(int i=0;i<=deg;i++) coeff[i] = P.coeff[i];
}
~Polynomial()
{
delete []coeff;
}
Polynomial operator+(const Polynomial& P) const;
Polynomial operator-(const Polynomial& P) const
{
int i;
Polynomial c;
if(deg>=P.deg)
{
c.deg=deg;
for(i=deg;i>=0;i--)
c.coeff[i]=coeff[i];
for(i=P.deg;i>=0;i--)
c.coeff[i]=c.coeff[i]-P.coeff[i];
}
else
{
c.deg=P.deg;
for(i=P.deg;i>=0;i--)
c.coeff[i]=-P.coeff[i];
for(i=deg;i>=0;i--)
c.coeff[i]=c.coeff[i]+coeff[i];
}
return c;
}
Polynomial& operator=(const Polynomial& P)
{
if (this!=&P)
{
delete [] coeff;
deg = P.deg;
coeff = new float[deg+1];
for(int i=deg;i>=0;i--) coeff[i] = P.coeff[i];
}
return (*this);
}
float operator[](float x) // evaluation of polynomial
{
float rez=0;
for(int i=deg;i>=0;i--)
rez=rez*x+coeff[i];
return rez;
}
int degree() const
{
return deg;
}
public:
float *coeff; // array- representation of polynomial
int deg; //degree of polynomial
friend ostream& operator<<(ostream &out, const Polynomial& P) //overload operatpr<<
{
for ( int i =P.deg; i >= 0; i-- )
{
out << P.coeff[i] << "x^" << i << " ";
if(i>=1)
{
if(P.coeff[i-1]>0) out<<"+";
}
}
out << endl;
return out;
}
};
Polynomial Polynomial::operator+(const Polynomial& P) const
{
int i;
Polynomial c;
if(deg>=P.deg)
{
c.deg=deg;
for(i=deg;i>=0;i--)
c.coeff[i]=coeff[i];
for(i=P.deg;i>=0;i--)
c.coeff[i]=c.coeff[i]+P.coeff[i];
}
else
{
c.deg=P.deg;
for(i=P.deg;i>=0;i--)
c.coeff[i]=P.coeff[i];
for(i=deg;i>=0;i--)
c.coeff[i]=c.coeff[i]+coeff[i];
}
return c;
}
Polynomial shift(Polynomial A, int x)
{
Polynomial c(A.deg+x);
int k=0;
for(int i=c.deg;i>=0;i--)
c.coeff[i]=0;
for(int i=A.deg;i>=0;i--)
{
c.coeff[c.deg-k]=A.coeff[i];
k++;
}
return c;
}
Polynomial mult4( Polynomial P, Polynomial Q)
{
if(P.deg==1)
{
Polynomial qw(2);
qw.coeff[0]=P.coeff[0]*Q.coeff[0];
qw.coeff[2]=P.coeff[1]*Q.coeff[1];
qw.coeff[1]=P.coeff[1]*Q.coeff[0]+Q.coeff[1]*P.coeff[0];
return qw;
}
else
{
Polynomial p1(((P.deg)/2)-1);
Polynomial p2(P.deg-(P.deg)/2);
Polynomial q1(((Q.deg)/2)-1);
Polynomial q2(Q.deg-(Q.deg)/2);
int k=0;
for(int i=p1.deg;i>=0;i--)
{
p1.coeff[i]=P.coeff[i];
}
for(int i=p2.deg;i>=0;i--)
{
p2.coeff[i]=P.coeff[P.deg-k];
k++;
}
for(int i=q1.deg;i>=0;i--)
{
q1.coeff[i]=Q.coeff[i];
}
k=0;
for(int i=q2.deg;i>=0;i--)
{
q2.coeff[i]=Q.coeff[P.deg-k];
k++;
}
Polynomial t1,t2,t3;
t1=mult4(p1+p2,q1+q2);
t2=mult4(p1,q1);
t3=mult4(p2,q2);
return(t2+shift(t1-t2-t3,P.deg/2)+shift(t3,2*(P.deg/2)));
}
}
int main()
{
float *p=new float[4];
for(int i=3;i>=0;i--)
p[i]=-1.1+i;
float *k=new float[5];
for(int i=4;i>=0;i--)
k[i]=-1.2+i;
Polynomial P(p,3);
Polynomial K(k,4);
cout<<mult4(P,K);
return 0;
}
You have several errors in your code.
You need a copy constructor. Familiarize yourself with The Rule Of Three.
There are many lines where you set the deg of a Polynomial but don't allocate memory for coeff. For example, in shift, you have:
Polynomial shift(Polynomial A, float x)
{
Polynomial c;
int k=0;
c.deg=A.deg+x;
for(int i=c.deg;i>=0;i--)
c.coeff[i]=0;
What you need is:
Polynomial shift(Polynomial A, float x)
{
// Polynomial c(A.deg+x);
// Not sure why you whether c.deg should be A.deg+x or just A.deg.
Polynomial c(A.deg);
int k=0;
for(int i=c.deg;i>=0;i--)
c.coeff[i]=0;
In the operator= function, you are changing the index incorrectly. In stead of using:
for(int i=deg;i>=0;i++) coeff[i] = P.coeff[i];
you need to use:
for(int i=deg;i>=0;i--) coeff[i] = P.coeff[i];
// ^^ Decrement, not increment
In mult4, your use of
p1.deg=((P.deg)/2)-1;
seems flawed. What happens when P.deg/2 is equal to 0? Perhaps you need to use:
p1.deg=(P.deg)/2;
Setting of q1.deg must be similarly updated.
I might have missed other errors.
I am trying to define a simple class to work with 2d matrices, called Matrix2D01, and having trouble with the + operator.
I have the += operator which is working fine,
Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
int i,j;
for (i=0;i<M;i++)
for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
The + operator is defined:
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result(1,1);
result=*this;
result+=mat2;
return result;
}
When i try to use the + operator, for example by
mat3=mat1+mat2;
the compiler gives an error:
../MatrixGames.cpp:17:12: error: no match for ‘operator=’ in ‘mat3 = Matrix2D01::operator+(Matrix2D01&)((* & mat2))’
If anyone can tell me what I'm doing wrong it will be greatly appreciated.
Thanks.
I also have a = operator defined,
Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
if (this==&mat2) return *this;
int i,j;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
M=mat2.Dimensions()[0];
N=mat2.Dimensions()[1];
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=mat2[i][j];
return *this;
}
here is a complete test case:
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <vector>
using namespace std;
class Matrix2D01 {
protected:
int D;
double **mat;
int M,N;
public:
Matrix2D01(int m,int n);
Matrix2D01(int m,int n,double d);
~Matrix2D01();
vector <int> Dimensions() {vector <int> d(D,M); d[1]=N; return d;}
double* operator[](int i) {return mat[i];}
Matrix2D01& operator=(Matrix2D01& mat2);
Matrix2D01& operator+=(Matrix2D01& mat2);
Matrix2D01 operator+(Matrix2D01& mat2);
};
Matrix2D01::Matrix2D01(int m, int n) {
int i,j;
D=2;
M=m;
N=n;
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=0;
}
Matrix2D01::Matrix2D01(int m, int n,double d) {
int i,j;
D=2;
M=m;
N=n;
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=d;
}
Matrix2D01::~Matrix2D01() {
int i;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
}
Matrix2D01& Matrix2D01::operator=(Matrix2D01& mat2) {
if (this==&mat2) return *this;
int i,j;
for (i=0;i<M;i++) {
delete [] mat[i];
}
delete [] mat;
M=mat2.Dimensions()[0];
N=mat2.Dimensions()[1];
mat = new double* [M];
for (i=0;i<M;i++) {
mat[i]=new double [N];
}
for (i=0;i<M;i++)
for (j=0;j<M;j++)
mat[i][j]=mat2[i][j];
return *this;
}
Matrix2D01& Matrix2D01::operator+=(Matrix2D01& mat2) {
int i,j,M2,N2;
M2=mat2.Dimensions()[0];
N2=mat2.Dimensions()[1];
if ((M!=M2)||(N!=N2)) {
cout<<"error: attempted to add non-matching matrices";
return *this;
}
for (i=0;i<M;i++)
for (j=0;j<N;j++) mat[i][j]+=mat2[i][j];
return *this;
}
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result(1,1);
result=*this;
result+=mat2;
return result;
}
int main() {
Matrix2D01 mat1(2,2,1);
Matrix2D01 mat2(2,2,2);
Matrix2D01 mat3(2,2,4);
mat3+=mat1;
mat3=mat1+mat2;
return 1;
}
Herb Sutter advocates the following canonical way to overload operator + (please read GotW, it is really well written and interesting):
Don't include operator + as a member function, instead make it a free function.
Pass one object by value and the other by const reference. This makes it possible to use move operators when you add to a temporary.
Implement in terms of operator +=:
Matrix2D01 operator+(Matrix2D01 a, const Matrix2D01 &b)
{
a += b;
return a;
}
I've posted the code of the Matrix class I had written for an assignment on Discrete Mathematics last Sem
Notice how the assignment operator and the copy constructor are implemented All you need to do is write an assignment operator (I'd recommend that you write a copy constructor as well)
#pragma once
#include <vector>
#include <istream>
#include <ostream>
class Matrix
{
private:
unsigned int rows, cols;
std::vector<std::vector<int> > matrix;
public:
// Creates a Matrix with the given dimensions and sets the default value of all the elements to 0
Matrix(unsigned int rows, unsigned int cols);
// Destructor
~Matrix();
// Converts the relation set into it's adjacency Matrix representation
static Matrix CreateFromRelationSet( std::vector<std::pair<int, int> > relationSet);
// Copy Constructor
Matrix(const Matrix& cSource);
// Assignment Operator
Matrix& operator=(const Matrix& cSource);
// Resets all the elements of the matrix to 0
void Reset();
// Resizes the matrix to the given size
void Resize(unsigned int rows, unsigned int cols);
// Returns the number of rows
unsigned int getRows();
// Returns the number of columns
unsigned int getCols();
// Returns the smallest element from the matrix
int getSmallestElement();
// Returns the greatest element from the matrix
int getGreatestElement();
// Returns true if element is found and sets the row and column
// Returns false if not found
bool getPosition(int element, int* i, int* j);
// Deletes a row from the Matrix
void DeleteRow(unsigned int row);
// Deletes a column from the Matrix
void DeleteColumn(unsigned int col);
// Returns the element at (i,j)
int& operator()(unsigned int i, unsigned j);
// Returns the element at (i,j). Use the () operators instead
int getElementAt(unsigned int i, unsigned j);
friend std::ostream& operator << (std::ostream& out, Matrix& m);
friend std::istream& operator >> (std::istream& in, Matrix& m);
Matrix operator + (Matrix M);
Matrix operator - (Matrix M);
Matrix operator * (Matrix M);
};
// For use with cin & cout
std::ostream& operator << (std::ostream& out, Matrix& m);
std::istream& operator >> (std::istream& in, Matrix& m);
#include "Matrix.h"
Matrix::Matrix(unsigned int rows, unsigned int cols)
{
this->rows = rows;
this->cols = cols;
matrix.resize(rows);
for(std::vector<int>& i: matrix)
i.resize(cols);
Reset();
}
Matrix::~Matrix()
{
}
// local helper function
int findMaxFromSet(std::vector<std::pair<int, int> > set)
{
int maxVal = 0;
for(unsigned int i = 0; i < set.size(); i ++)
{
int p = set[i].first > set[i].second ? set[i].first : set[i].second;
maxVal = maxVal > p ? maxVal : p;
}
return maxVal;
}
Matrix Matrix::CreateFromRelationSet (std::vector<std::pair<int, int> > relationSet)
{
int max = findMaxFromSet(relationSet);
Matrix M(max,max);
M.Reset();
for(auto i = relationSet.begin(); i != relationSet.end(); ++ i)
M(i->first - 1, i->second - 1) = 1;
return M;
}
void Matrix::Reset()
{
for (auto& i: matrix)
for(auto& j: i)
j = 0;
}
void Matrix::Resize(unsigned int rows, unsigned int cols)
{
matrix.resize(rows);
for(auto& i:matrix)
i.resize(cols);
}
unsigned int Matrix::getRows()
{
return rows;
}
unsigned int Matrix::getCols()
{
return cols;
}
Matrix::Matrix(const Matrix& cSource)
{
rows = cSource.rows;
cols = cSource.cols;
matrix = cSource.matrix;
}
bool Matrix::getPosition(int element, int* i, int* j)
{
for(unsigned int ii = 0; ii < getRows(); ii ++)
for(unsigned int jj = 0; jj < getCols(); jj ++)
if(matrix[ii][jj] == element)
{
*i = ii;
*j = jj;
return true;
}
return false;
}
Matrix& Matrix::operator=(const Matrix& cSource)
{
// check for self-assignment
if (this == &cSource)
return *this;
if(this->getRows() < cSource.rows)
{
this->rows = cSource.rows;
this->matrix.resize(cSource.rows);
}
if(this->getCols() < cSource.cols)
{
this->cols = cSource.cols;
for(auto& i:this->matrix)
i.resize(cSource.cols);
}
for(unsigned int i = 0; i < rows; i++)
for(unsigned int j = 0; j < cols; j++)
this->matrix[i][j] = const_cast<Matrix&>(cSource)(i,j);
return *this;
}
std::ostream& operator << (std::ostream& out, Matrix& m)
{
for(auto& i: m.matrix)
{
for(auto& j: i)
out<<j<<'\t';
out<<std::endl;
}
return out;
}
std::istream& operator >> (std::istream& in, Matrix& m)
{
for(auto& i: m.matrix)
for(auto& j: i)
in>>j;
return in;
}
Matrix Matrix::operator + (Matrix op)
{
// Find the rows and cols of the new matrix
unsigned int r = this->getRows() > op.getRows()?this->getRows():op.getRows();
unsigned int c = this->getCols() > op.getCols()?this->getCols():op.getCols();
// Create Matrices
Matrix A = *this;
Matrix B = op;
Matrix R(r,c);
// Assign values
for(unsigned int i = 0; i < A.rows; i++)
for(unsigned int j = 0; j < A.cols; j++)
R(i,j) = A(i,j) + B(i,j);
return R;
}
Matrix Matrix::operator - (Matrix op)
{
// Find the rows and cols of the new matrix
unsigned int r = this->getRows() > op.getRows()?this->getRows():op.getRows();
unsigned int c = this->getCols() > op.getCols()?this->getCols():op.getCols();
// Create Matrices
Matrix A = *this;
Matrix B = op;
Matrix R(r,c);
// Assign values
for(unsigned int i = 0; i < A.rows; i++)
for(unsigned int j = 0; j < A.cols; j++)
R(i,j) = A(i,j) - B(i,j);
return R;
}
Matrix Matrix::operator* (Matrix op)
{
Matrix A = *this;
Matrix B = op;
if(A.getCols() != B.getRows())
throw std::exception("Matrices cannot be multiplied");
Matrix M(A.getRows(), B.getCols());
for(unsigned int i=0 ; i<A.getRows() ; i++)
for(unsigned int j=0 ; j<B.getCols() ; j++)
for(unsigned int k=0 ; k<B.getRows() ; k++)
M(i,j) = M(i,j) + A(i,k) * B(k,j);
return M;
}
int& Matrix::operator()(unsigned int i, unsigned j)
{
return matrix[i][j];
}
int Matrix::getElementAt(unsigned int i, unsigned j)
{
return (*this)(i,j);
}
int Matrix::getSmallestElement()
{
int result = matrix[0][0];
for(auto i:matrix)
for(auto j : i)
if(j < result)
result = j;
return result;
}
int Matrix::getGreatestElement()
{
int result = matrix[0][0];
for(auto i:matrix)
for(auto j : i)
if(j > result)
result = j;
return result;
}
void Matrix::DeleteRow(unsigned int row)
{
matrix.erase(matrix.begin() + row);
rows --;
}
void Matrix::DeleteColumn(unsigned int col)
{
for(auto& i: matrix)
i.erase(i.begin() + col);
cols --;
}
UPDATE:
result=*this;
In your code you are trying to allocate result the value of *this using the assignment operator, but no assignment operator is defined and the compiler doesn't find a match
Writing an assignment operator overload will solve this issue.
And if you have a copy constructor you can do something like:
Matrix2D01 Matrix2D01::operator+(Matrix2D01& mat2) {
Matrix2D01 result = *this;
result+=mat2;
return result;
}
Writing Assignment Operators: http://www.learncpp.com/cpp-tutorial/912-shallow-vs-deep-copying/
And if you're interested in the details - here you go: http://www.icu-project.org/docs/papers/cpp_report/the_anatomy_of_the_assignment_operator.html
Your operands should be const references. Otherwise, you
can't use a temporary to initialize them. (In this case, the
return value of operator+ is a temporary, and thus, cannot be
bound to the non-const reference of operator=.)
If your operator+ is a member, it should be const as well.
After all, it doesn't modify the object it is called on. So:
Matrix2D01& Matrix2D01::operator=( Matrix2D01 const& mat2 );
Matrix2D01& Matrix2D01::operator+=( Matrix2D01 const& mat2 );
Matrix2D01 Matrix2D01::operator+( Matrix2D01 const& mat2 ) const;
Also, your assignment operator is broken. (Think of what will
happen, if one of the allocations fails after you've freed the
original data.) In general, if you have to test for self
assignment, the assignment operator is broken.