Polynomial code - c++

I'm working on an assignment for my C++ class and have run into a little problem when running the program. I get an error stating Unhandled exception at 0x000944C8 in Pog11.exe: 0xC0000005: Access violation writing location 0x00000000. while debugging. The goal is to read in the int degree of a polynomial, as well as the double coefficients.
here is the .h file that I was supplied:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include<iostream>
using std::ostream;
using std::istream;
using std::cerr;
using std::endl;
class Polynomial
{
friend ostream& operator<<( ostream& left , const Polynomial& right);
friend istream& operator>>( istream& left , Polynomial& right );
public:
Polynomial();
Polynomial( int degree, const double* coefficients );
Polynomial( const Polynomial& );
~Polynomial();
const Polynomial& operator=( const Polynomial& deg);
bool operator==( const Polynomial& deg ) const;
void setDegree(int d);
int getDegree() const;
private:
int degree;
double* coefficients;
};
#endif
And here is the segment of code that is causing the error:
istream& operator>>(istream& left, Polynomial& right)
{
int tmp;
left >> tmp;
right.setDegree(tmp);
int i = 0;
while (i<=right.getDegree())
{
double co;
left >> co;
right.coefficients[i] = co;
i++;
}
return left;
}
Specifically the right.coefficients[i]=co; line is what causes the program to crash.
Here are the constructors for the class:
#include "Polynomial.h"
Polynomial::Polynomial() :degree(0), coefficients(0)
{
degree = 0;
coefficients = new double[degree];
}
Polynomial::Polynomial(int deg, const double* coefficients)
{
if (deg < 0)
{
degree = 0;
}
else
{
degree = deg;
}
coefficients = new double [degree];
}
Polynomial::Polynomial(const Polynomial& deg)
{
if (deg.getDegree() <= 0)
{
setDegree(0);
}
else
{
setDegree(deg.getDegree());
for (int i = 0; i < degree; i++)
{
coefficients[i] = deg.coefficients[i];
}
}
}

There's code missing - e.g. the implementation of the constructor of the Polynomial object.
I'm pretty sure that your error is that you have not allocated (enough) memory for the coefficients data member. There must be a
coefficients = new double[number_of_coeffs]
somewhere in your code.
EDIT
There's a number of points where you need to do this: where the degree of the polynomial is (re)set. Because then you know the degree of the polynomial:
Here you must copy the elements passed:
Polynomial( int degree, const double* coefficients ):
coefficients( new double[degree] ), degree( degree )
{
::memcpy(this->coefficients, coefficients, degree * sizeof(double));
}
and in this one, the degree of the polynomial changes - so your coefficients array must be modified accordingly.
Polynomial::setDegree( int degree ) {
// first delete the old coefficients
delete [] coeffs;
// and make a new array
this->degree = degree;
this->coefficients = new double[ this->degree ];
// initialize the coefficients
..
}
Similar actions have to be done in the copy constructor and the assignment operator.
Finally: you might be better off using std::vector<double> which basically does all the memory management for you. See http://en.cppreference.com/w/cpp/container/vector

Related

C++ Creating an array then populating it with elements of a class

In my class we are creating a dynamic array and learning how to use free store and de-allocating data correctly. We are creating a Line container that holds a Point class which contains points on a graph, this assignment is building off of another assignment of ours but uses new Line member functions.
I am currently having issues with actually creating the array and populating it with coordinates of points. I am not really sure how to begin writing the Main(), what my idea is so far is to create the Line array that will contain members of my Point class. I want to initialize 5 elements and then add 6 so it will create a new array with more elements.
So far I have the following thought on how to start it.
Line* points = new Line[5];
for ( int i = 0; i < 5; i++ ){
points[i] = 0; //This causes "no match for ‘operator=’ (operand types are ‘Line’ and ‘int’)"
}
Then I would probably try to assign points with members of my point class
Point pointOne(1.0, 1.0)
I've tried adding elements like the following but its incorrect.
points[0] -> (1.0, 1.0);
points[0] = pointOne;
Here are my .h and .cpp files
Line.cpp
#include "Point.h"
#include "Line.h"
#include <stdexcept>
//default constructor
Line::Line(){
size();
index = 0;
points = new Point[size()];
Point::Point();
}
//destructor
Line::~Line(){
delete[] points;
}
void Line::push_back(const Point& p){
if(index == size()) // If array is too small copy all data to a new array, reassign pointer
{
Point * temp = new Point[size() + 5]; //temp array in free store
for(size_t i = 0; i < size(); i++)
{
temp[i] = points[i];
}
delete[] points; //clear array
points = temp; //reassign values
points[index++] = p; //Places p into array
delete[] temp;
points[size()] = (5 + size());
}
else
{
points[index++] = p;
}
}
/**
* Clear the list of points by setting index to 0.
*/
void Line::clear(){
delete[] points;
}
/**
* Return the length of the line. The length is calculated as
* the sum of the distance between all points in the line.
*/
double Line::length(){
double total = 0.0;
for (unsigned int index = 0; (index+1) < 10; index++){ //Loop for index and index+1 to prevent going out of the vector
total += points[index].distance(points[index+1]); //Add up distance (A to B) + (B to C) etc
}
return total;
}
/**
* return the number of Points contained in our line
*/
unsigned int Line::size() const{
int i = 5;
return i;
}
/**
* [] operator override
*/
Point & Line::operator[](int index){ //rewrite out-of-bounds
if(index == size())
{
throw std::out_of_range("Out of bounds.");
}
return points[index];
}
Line.h
#ifndef LINE_H_
#define LINE_H_
#include "Point.h"
class Line {
public:
/**
* Constructor and destructor
*/
Line();
virtual ~Line();
/**
* Add a point to the end of our line. If the line contains
*/
void push_back(const Point& p);
/**
* Clear the list of points
*/
void clear();
/**
* Return the length of the line. The length is calculated as
* the sum of the distance between all points in the line.
*/
double length();
/**
* return the number of Points in our line
*/
unsigned int size() const;
/**
* [] operator override
*/
Point & operator[](int index);
private:
unsigned int index; //index of array
Point *points; //Pointer to data
};
#endif /* LINE_H_ */
Point.cpp
#include "Point.h"
#include "Line.h"
#include <cmath>
#include <stdexcept>
/** default constructor **/
Point::Point(){
// TODO Auto-generated constructor stub
this->x = 0.0;
this->y = 0.0;
}
//Overloaded Constructor
Point::Point(const double x, const double y){
// TODO Auto-generated constructor stub
this->x = x;
this->y = y;
}
/**
* Copy constructor */
Point::Point(const Point & t){
x = t.x;
y = t.y;
}
//Default destructor
Point::~Point() {
// TODO Auto-generated destructor stub
}
//Retrieve X value
double Point::getX() const {
return x;
}
//Retrieve Y value
double Point::getY() const {
return y;
}
//Function for finding distance between 2 points on a graph. Using ((x-p.x)^2+(y-p.y)^2)^1/2
double Point::distance(const Point &p) const{
return sqrt( pow((x - p.getX()),2) + pow((y - p.getY()),2));
}
/**
* Output the Point as (x, y) to an output stream
*/
std::ostream& operator <<(std::ostream &out , const Point &point){
out <<'(' << point.x << ',' << ' ' << point.y << ')';
return out;
}
/**
* Declare math operators
*/
//Addition
Point operator +(const Point &lhs, const Point &rhs){
return Point(lhs.x + rhs.x, lhs.y + rhs.y);
}
//Subtraction
Point operator -(const Point &lhs, const Point &rhs){
return Point(lhs.x - rhs.x, lhs.y - rhs.y);
}
/**
* Copy assignment
*/
Point & Point::operator =(const Point& rhs){
x= rhs.x;
y= rhs.y;
return *this;
}
Point.h
#ifndef POINT_H_
#define POINT_H_
#include <iostream>
class Point {
public:
/**
* Constructor and destructor
*/
Point();
Point(const double x, const double y);
virtual ~Point();
/**
* Get the x value
*/
double getX() const;
/**
* Get the y value
*/
double getY() const;
/**
* Return the distance between Points
*/
double distance(const Point &p) const;
/**
* Output the Point as (x, y) to an output stream
*/
friend std::ostream& operator <<(std::ostream &out, const Point &point);
/**
* Declare comparison relationships
*/
friend bool operator ==(const Point &lhs, const Point &rhs);
friend bool operator <(const Point &lhs, const Point &rhs);
/**
* Declare math operators
*/
friend Point operator +(const Point &lhs, const Point &rhs);
friend Point operator -(const Point &lhs, const Point &rhs);
/**
* Copy constructor
*/
Point(const Point& t);
/**
* Copy assignment
*/
Point &operator =( const Point& rhs );
private:
double x;
double y;
};
#endif /* POINT_H_ */

Overloading the [] operator

I am a beginner in C++. I am learning on how to overload operators. I have created a class Complex that represents complex numbers and methods for complex arithmetic and a class ComplexArray that represents fixed-length arrays of elements in complex vector space C.
I get compiler errors, that it is unable to find the correct form of operator[]. However, I searched the internet and I am unable to rectify the error. Any hints/tips in the right direction would be of tremendous help.
Severity Code Description Project File Line Suppression State
Error C2676 binary '[': 'const ComplexArray' does not define this operator or a conversion to a type acceptable to the predefined operator ComplexArrays c:\users\quasa\source\repos\complexarrays\complexarrays\testcomplexarray.cpp 7
Here is my code:
TestComplexArray.cpp
#include <iostream>
#include "ComplexArray.h"
Complex ComplexSum(const ComplexArray& cArray, int size)
{
Complex sum = cArray[0];
for (int i = 1; i < size; i++)
{
sum = sum + cArray[i];
}
return sum;
}
Complex ComplexProduct(const ComplexArray& cArray, int size)
{
Complex product = cArray[0];
for (int j = 1; j < size; j++)
{
product = product * cArray[j];
}
return product;
}
int main()
{
char ch;
const int size = 5;
ComplexArray cArray(size);
for (int i = 0; i < size; i++)
{
cArray[i] = Complex((double)(i + 1), 0);
std::cout << cArray[i];
}
Complex sum = ComplexSum(cArray, size);
Complex product = ComplexProduct(cArray, size);
std::cout << "Sum = " << sum << std::endl;
std::cout << "Product = " << product << std::endl;
std::cin >> ch;
return 0;
}
ComplexArray.h
class ComplexArray
{
private:
Complex* complexArr;
int size;
ComplexArray();
public:
//Constructors and destructors
ComplexArray(int size);
ComplexArray(const ComplexArray& source);
virtual ~ComplexArray();
//Range for the complexArr
int MaxIndex() const;
//Overload the indexing operator
const Complex& operator [](int index) const;
Complex& operator [](int index);
};
ComplexArray.cpp
#include "Complex.h"
#include "ComplexArray.h"
ComplexArray::ComplexArray(int s)
{
size = s;
complexArr = new Complex[size];
}
ComplexArray::ComplexArray(const ComplexArray& source)
{
//Deep copy source
size = source.size;
complexArr = new Complex[size];
for (int i = 0; i < size; i++)
{
complexArr[i] = source.complexArr[i];
}
}
ComplexArray::~ComplexArray()
{
delete[] complexArr;
}
int ComplexArray::MaxIndex() const
{
return (size - 1);
}
/*
c1.operator[](int index) should return a reference to the Complex
object, because there are two possible cases.
Case 1:
Complex c = complexArray[3];
Case 2:
complexArray[3] = c;
In the second case, complexArray[3] is an lvalue, so it must return
a Complex object by reference, so that it can be assigned to.
*/
const Complex& ComplexArray::operator[] (int index) const
{
return complexArr[index];
}
Complex& ComplexArray::operator[](int index)
{
return complexArr[index];
}
Complex.h
#include <iostream>
class Complex
{
private:
double x;
double y;
void init(double xs, double ys); //Private helper function
public:
//Constructors and destructors
Complex();
Complex(const Complex& z);
Complex(double xs, double ys);
virtual ~Complex();
//Selectors
double X() const;
double Y() const;
//Modifiers
void X(double xs);
void Y(double ys);
//Overload binary +, = and * operators
Complex operator + (const Complex& z);
Complex& operator = (const Complex& z);
Complex operator * (const Complex& z) const;
//Overload unary - operator
Complex operator -() const;
friend Complex operator * (const double alpha, const Complex& z);
friend Complex operator * (const Complex& z, const double beta);
//Overload << operator
friend std::ostream& operator << (std::ostream& os, const Complex& z);
//A complex function f(z)=z^2
Complex square();
};
As you have all pointed out - I was missing the forward definition of a #include.
Complex.cpp has the header
#include "Complex.h"
ComplexArray.h has the header
#include "Complex.h"
ComplexArray.cpp has the header
#include "ComplexArray.h"
TestComplexNumbers.cpp has the header
#include <iostream>
#include "ComplexArray.h"
My compile-time errors have been resolved.
I don't think the error comes from operator[], as you can see in the function:
Complex ComplexSum(const ComplexArray& cArray, int size)
{
Complex sum = cArray[0];
for (int i = 1; i < cArray.MaxIndex(); i++)
{
sum = sum + cArray[i];
}
}
You don't return a result. That's fatal.
ComplexArray depends on Complex but order of includes doesn't look right
#include "ComplexArray.h"
#include "Complex.h"
You have to forward-declare Complex before ComplexArray
class Complex;
Code fails at \testcomplexarray.cpp line 7 which is
Complex sum = cArray[0];
It looks like you have problem with ctors of Complex. Be sure that you have NOT defined such:
Complex(Complex& v); // that's bad. it prevents to use copy constructor
If you need copy ctor for some inconceivable reason, it always should look so:
Complex(const Complex& v);

Overloading i/o operators in C++ for polynomials

I got this project where I have to overload the i/o operators to read and write polynomials. Unfortunately I can't seem to get it to work.
I have the header file:
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
class Polynomial
{
public:
Polynomial();
Polynomial(int degree, double coef[]);
int degree;
double coef[ ];
friend istream& operator>>(istream&,Polynomial& );
friend ostream& operator<<(ostream&,const Polynomial&);
virtual ~Polynomial();
};
#endif // POLYNOMIAL_H
and the cpp file:
#include "Polynomial.h"
#include<iostream>
#include<string>
using namespace std;
Polynomial::Polynomial()
{
//ctor
}
Polynomial::~Polynomial()
{
//dtor
}
Polynomial::Polynomial(int d, double c[])
{
degree = d;
double coef [degree+1];
for(int i = 0; i < d+1; i++)
{
coef[i] = c[i];
}
}
istream& operator>>(istream& x, const Polynomial& p)
{
cout<<"The degree: ";
x>>p.degree;
for(int i = 0; i < p.degree+1; i++)
{
cout<<"The coefficient of X^"<<i<<"=";
x>>p.coef[i];
}
return x;
}
ostream& operator<<(ostream& out, const Polynomial& p)
{
out << p.coef[0];
for (int i = 1; i < p.degree; i++)
{
out << p.coef[i];
out << "*X^";
out << i;
}
return out;
}
In the name I am trying to read a polynomial and then to write another one:
#include <iostream>
using namespace std;
#include "Polynomial.h"
int main()
{
Polynomial p1();
cin >> p1;
int degree = 2;
double coef [3];
coef[0]=1;
coef[1]=2;
coef[3]=3;
Polynomial p(degree, coef);
cout<<p;
return 0;
}
When I run the program it just freezes and I can't seem to find the error.
Any ideas?
Polynomial::Polynomial(int d, double c[])
{
degree = d;
double coef [degree+1];
for(int i = 0; i < d+1; i++)
{
coef[i] = c[i];
}
}
Here, you create local array coef (with non-standard C++ btw) and then assign to it. Your member coeff is not initialized to anything meanigfull (and makes little sense the way it is right now in the first place).
Instead of double coef[] you should use std::vector like this:
struct polynomial {
std::vector<double> coef;
// No need for member varaible degree, vector knows its lengths
polynomial (const std::vector<double> &coeffs) : coef (coeffs) {}
};
And then define all other constructors you need to do something meaningful. Alternatively, you can leave out constructors entirely and directly assign the coefficient vector. Then you can for example functions like
int degree (const polynomial &p) {
return p.coef.size() - 1;
}
or
std::ostream &operator << (std::ostream &out, const polynomial p) {
if (p.coef.size() == 0) {
out << "0\n";
return out;
}
out << p.coeff[0];
for (int i = 1; i < p.coef.size(); ++i)
out << " + " << p.coef[i] << "*X^i";
out << "\n";
return out;
}
(1)
double coef[];
This is non-standard approach to have un-sized/dynamic-sized array on stack. You better give the array some size, OR make it a pointer and allocate memory yourself; OR use vector<double>
(2)
You are creating a local variable in constructor, which will hide the member-variable in class. If you are using pointer approach, you need to allocate it properly here in constructor. With either approach, you should initialize the (member) variable coef with zeros, ideally.
(3)
Polynomial p1();
This effectively declares a function named p1 which would return a Polynomial and not a variable of tyoe Polynomial. You may want to use:
Polynomial p1;

Unable to fix a Access Violation reading location error

So, I've looked around for an answer for a while, trying to solve this error. I think I know what the problem is, but I still can't find where or how it's happening exactly.
The error is that I'm getting a Access Violation at the location 0xcccccccc. My searching has lead me to that the problem is that I'm trying to delete a pointer that either doesn't exist, which I don't believe, or that I'm deleting a temporary variable. The problem is that I can't figure out which it is. I'm sure that once it's pointed out I'll feel stupid, but I REALLY can't seem to see this.
Pertinent Code down the callstack from the top.
// Minimum Polynomial Function
matrix matrix::MIN_POLY()
{
// Get stuff ready to find the Minimal Polynomial of the matrix
// Get our I matrix in.
col[0] = MatrixPow( *this, 0); // Goes in here <-
// ... Rest of the function ...
}
// Matrix Power Function
matrix matrix::MatrixPow(matrix& A, int pow)
{
// Create an identity matrix should the pow == 0
matrix ret(A.size[0], A.size[1]);
for(int i = 0; i < ret.size[0]; ++i)
{
ret.data[i][i] = 1;
}
for( int i = 0; i < pow; ++i)
{
// multiply it by the number of times given, and 0 returns I
ret = MatrixMult(ret, A);
}
// Ret.data exists here still and is ready to be moved and deleted.
return ret; // Seems to call Copy around here for some reason. <------
}
// Copy constructor
matrix::matrix(const matrix& other)
{
// Ret.data is gone by here for some reason.
if(data)
{
for(int i = 0; i < size[0]; ++i)
{
delete[] data[i]; // Error happens in one of these deletes <-----
}
delete[] data;
}
// Rest doesn't matter as it stops above
}
And the matrix.h since it was asked for.
#include <complex>
class matrix
{
private:
std::complex<float> **data; // [row][column]
int size[2]; // 0 is rows/ 1 is columns
public:
// Default Constructor
matrix();
// Default Destructor
~matrix();
// Default Copy
matrix(const matrix& other);
// Premade matrix constructor
matrix(int row, int col, std::complex<float>** mat);
// Assignment operator to get stuff done
matrix& operator=(const matrix& other);
// Set function for the matrix
void Set(int row, int col, std::complex<float> dat);
// Get function for the matrix, inline for ease.
std::complex<float> Get(int row, int col)
{return data[row][col];}
// Get function for the number of rows
int GetRows()
{return size[0];}
// Get function for the number of columns
int GetCol()
{return size[1];}
// Scalar Multiplication
void ScalarMult(std::complex<float> scalar);
// Matrix Multiplication
matrix MatrixMult(const matrix& A, const matrix& B);
// Matrix Power series
matrix MatrixPow(matrix& A, int pow);
// Matrix addition
matrix MatrixAdd(const matrix& lhs, const matrix& rhs);
// Row op addition
void RowOp(int row1, int row2, std::complex<float> val1, std::complex<float> val2);
// Row op Swap
void RowSwap(int row1, int row2);
// RREF function
void RREF();
// minimal Polynomial
matrix MIN_POLY();
};
You define copy constructor:
matrix::matrix(const matrix& other)
Do not initialize members:
matrix::matrix(const matrix& other)
{
//body...
}
And then perform such check:
if(data)
Which by all means cannot be false - all pointers all initialized to compiler-specific sentinel value to indicate uninitialized state and help detect bugs like this. This usually applies to debug builds, unless you enable this also in release builds.
Solution:
1. Initialize data:
matrix::matrix(const matrix& other)
: data(NULL)
{
//body...
}
2. Do not perform this unnecessary check:
matrix::matrix(const matrix& other)
: data(NULL)
{
// copy size
size[0] = other.size[0];
size[1] = other.size[1];
// Make the numbers match
data = new std::complex<float>*[size[0]];
for(int i = 0; i < size[0]; ++i)
{
data[i] = new std::complex<float>[size[1]];
for(int j = 0; j < size[1]; ++j)
{
data[i][j] = other.data[i][j];
}
}
return;
}

Operator overloading with pointers

Hi there c++ programmers. I am encountering issue that i cant understand.I have strip the following program for readability reasons and left what i am having trouble with. I am trying to overload a + and = operators, but with dynamically created arrays. By themselves the + and = operator methods are producing the right results. However when i try to assign the result from the + operator to *poly3 i get "*poly3 need to be initialized" from the compiler. If i do initialize it nothing gets assign to it( i mean the result from +).
My question, what is the right way to do this. I need the result poly3 to be dynamic array or a pointer as well so i can use it latter.
Thanks a lot for the help in advance.
class Polynomial
{
private:
int *poly;
int size;
public:
Polynomial();
Polynomial(int);
Polynomial(string,int);
~Polynomial();
void setPoly(string);
int *getPoly() const;
Polynomial operator+(Polynomial&);
void operator=(const Polynomial&);
};
Polynomial::Polynomial(string polyInput, int s)
{
size = s+1;
poly = new int[size];
//set all coef position to 0
for(int i = 0; i < size; i++){
*(poly + i) = 0;
}
setPoly(polyInput);
}
Polynomial Polynomial::operator+(Polynomial &polyRight)
{
Polynomial *result = new Polynomial(size);
for(int i = 0; i < size; i++)
result->poly[i] = poly[i] + polyRight.poly[i];
return *result;
}
void Polynomial::operator=(const Polynomial &polyRight)
{
size = polyRight.size;
for(int i = 0; i < size; i++){
*(poly + i) = polyRight.poly[i];
}
}
int main()
{
int highestExp = 4;
Polynomial *poly1;
Polynomial *poly2;
Polynomial *poly3;// = new Polynomial(highestExp); // for the result
string input1,input2;
ifstream inputFile("data.txt");
getline(inputFile, input1);
getline(inputFile, input2);
poly1 = new Polynomial(input1,highestExp);
poly2 = new Polynomial(input2,highestExp);
*poly3 = *poly1 + *poly2;
system("pause");
return 0;
}
The assignment operator must return a reference to the instance assigned to:
Polynomial& operator=(const Polynomial&);
In the implementation, you should return *this;. You must also make sure that the implementation is robust against assignment of polynomials of different size. That doesn't seem to be the case currently.
Then, in main, poly3 is uninitialized. Just drop the pointers and instantiate all the polys as automatic storage, local variables in main():
Polynomial poly1(input1,highestExp);
Polynomial poly2(input2,highestExp);
Polynimial poly3 = poly1 + poly2;
As an aside, your operator+ has a memory leak. You should not be using new int it. Create a local Polynomial and return it.
Furthermore, you have no destructor taking care of releasing resources, so every instantiation of a Polynomial results in a memory leak. Your class has too many responsibilities: managing resources and being a polynomial. You should let a different class take care of the memory management. Use std::vector<int>, or, if this is an exercise, write your own dynamic array-like class, and use that inside Polynomial.
Pointers point to a memory location explicitly assigned to it.
Ptr* pt = new Ptr();//pt points to a valid address in memory
Ptr* pt;//pt has not yet been initialized
i.e. it is not pointing to any valid address. Using pt can cause unexpected behavior. By default pointers should point to NULL and they should be checked for !NULL and then used.
In above code Polynomial *poly3; is not pointing to any location and when we do
*pol3 we are actually dereferencing a location which had never been created.
When you do Polynomial *poly3 = new Polynomial(highestExp);, you are pointing poly3 to some valid location and hence *pol3 = *poly1 + *pol2 makes sense. But beware when you do a new for poly3 you are creating one more heap storage, so you have to make sure that you free all memory assigned on heap.
A better solution would be have your opertor + return a valid pointer and have it mapped to poly3 like this:
Polynomial* Polynomial::operator+(Polynomial &polyRight)
{
Polynomial *result = new Polynomial(size);
for(int i = 0; i < size; i++)
result->poly[i] = poly[i] + polyRight.poly[i];
return result;
}
//and in main
poly3 = (*poly1 + *poly2);
To start with:
// allow adding const
const Polynomial Polynomial::operator+(const Polynomial &polyRight) const
{
Polynomial result(size); // don't use a pointer
for(int i = 0; i < size; i++)
result.poly[i] = poly[i] + polyRight.poly[i];
return result;
}
make the return const to avoid the silliness of (A+B)=C;
Polynomial& Polynomial::operator=(const Polynomial &polyRight) // return a reference
{
size = polyRight.size;
for(int i = 0; i < size; i++){
*(poly + i) = polyRight.poly[i];
}
return *this; // allow chaining
}
Note that once you patch this up that you need to guard against A=A in operator =() :
if (this == &rhs)
{
// do stuff
}
return *this;
Here is quite a cleanup, with some slightly different functionality and better memory management. Since I didn't know the format of your input file, I've skipped the reading from file part, and just use a new constructor of the form Polynomial(countOfExponents, x0, x1, x2, x3, ... , xn)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdarg>
using namespace std;
class Polynomial
{
private:
int *poly;
int size;
public:
Polynomial() : size(0), poly(NULL) {}
Polynomial(int size, ... );
~Polynomial() { delete[](poly); }
void allocate(int size) {
if (NULL!=poly) {
delete[](poly);
}
Polynomial::size = size;
poly=new int[size];
}
Polynomial operator+(const Polynomial&) const;
Polynomial &operator=(const Polynomial&);
int exponent(int p) const {
return (p<size) ? poly[p] : 0;
}
string str() const;
};
Polynomial::Polynomial(int size, ...) : size(size) {
va_list varargs;
va_start(varargs, size);
poly = new int[size];
for (int i=0; i<size; i++) {
poly[i] = va_arg(varargs, int);
}
va_end(varargs);
}
Polynomial Polynomial::operator+(const Polynomial &polyRight) const
{
int newSize = max(size, polyRight.size);
Polynomial result;
result.allocate(newSize);
for(int i = 0; i < newSize; i++)
result.poly[i] = exponent(i) + polyRight.exponent(i);
return result;
}
Polynomial &Polynomial::operator=(const Polynomial &polyRight)
{
allocate(polyRight.size);
memcpy(poly, polyRight.poly, sizeof(int) * size);
return *this;
}
string Polynomial::str() const {
stringstream out;
for (int i=size-1; i>=0; i--) {
out << poly[i];
if (0<i) {
out << " ";
}
}
return out.str();
}
int main()
{
Polynomial one(3, 1, 2, 3);
Polynomial two(3, 2, 3, 4);
cout << one.str() << endl;
cout << two.str() << endl;
Polynomial three = one + two;
cout << three.str() << endl;
return 0;
}
Note that I'm also being careful not to address memory that might not exist when working with polynomials of different sizes. Accessing poly[n] on a polynomial with fewer than n exponents would cause trouble. Instead, use the exponent(n) function, which will return 0 for all exponents higher than those inside the polynomial.