I'm trying to write a class handling multiplication, division, etc of polynomials.
In my header file, I have my class definition, in which I have those public functions :
void Poly_Mult(const Polynomial& Poly);
void Poly_Divide(const Polynomial& Poly);
and also two friend functions :
friend Polynomial Poly_Mult(const Polynomial& Poly1, const Polynomial& Poly2);
friend Polynomial Poly_Divide(const Polynomial& Poly1, const Polynomial& Poly2);
I have defined both Poly_Mult functions with no problem and I try to use them to define the Poly_Divide ones.
For the friend function, I have no problem. However, in the member function :
bool Polynomial::Poly_Divide(const Polynomial& Poly)
{
if (Poly.m_Degree == 0)
return false;
Polynomial result;
result.m_Degree = this->m_Degree - Poly.m_Degree;
result.m_Variable = this->m_Variable;
Polynomial reduced(*this);
double GuidingFactor = Poly.m_Terms[Poly.size() - 1].Factor();
while (reduced.m_Degree >= Poly.m_Degree)
{
int exp = reduced.m_Degree - Poly.m_Degree;
double termFactor = reduced.m_Terms[reduced.size() - 1].Factor() / GuidingFactor;
//I get an error on this next line. The compiler doesn't seem to find to right overloaded function for Poly_Mult
reduced.Poly_Subtract(Poly_Mult(Poly, Polynomial(termFactor, Poly.m_Variable, exp)));
result.m_Terms.push_back(VarPower(termFactor, exp));
reduced.Simplify();
}
result.Simplify();
*this = result;
if(reduced.size() == 1 && reduced.m_Terms[0].Factor() == 0 && reduced.m_Terms[0].Exponent() == 0)
return true;
else
return false;
}
The code is functionally the same in the friend function and it works perfectly as intended, no errors.
instead of the whole detailed function since the friend function works fine, but again, the compiler doesn't seem to find to right overloaded function.
I really don't see the problem here, as the functions have different parameters so I see no ambiguity.
By simplifying the code, I get the same problem with this :
in my header file, I have my class definition :
class Polynomial
{
private:
int m_Degree;
public:
Polynomial(int degree)
: m_Degree(degree) {};
Polynomial(const Polynomial& Poly)
: m_Degree(Poly.m_Degree) {};
Polynomial& operator=(const Polynomial& Poly)
{
m_Degree = Poly.m_Degree;
}
void Multiply(const int& number);
friend Polynomial Multiply(const Polynomial& Object, const int& number);
void Divide(const int& number);
friend Polynomial Divide(const Polynomial& Object, const int& number);
};
Then, in my cpp file, I have the implementation of the functions :
#include "Polynomial.h"
void Polynomial::Multiply(const int& number)
{
m_Degree *= number;
}
Polynomial Multiply(const Polynomial& Object, const int& number)
{
return Polynomial(Object.m_Degree * number);
}
void Polynomial::Divide(const int& number)
{
Polynomial copy = *this;
Polynomial result = Multiply(copy, (1./number)); //This is where the problem is. The compiler doesn't find the right overloaded function
*this = result;
}
Polynomial Divide(const Polynomial& Object, const int& number)
{
Polynomial result = Multiply(Object, (1. / number));
return result;
}
I get those errors :
error C2660: 'Polynomial::Multiply': function does not take 2 arguments
no suitable constructor exists to convert from "void" to "Polynomial"
Related
I have created a Polynomial class but I want to overload indexing operator to be used as a setter, for example, myPolyObject[0] = 2.5 but It's giving me an error when trying to overload it.
class Polynomial
{
public:
Polynomial();
Polynomial(Polynomial&);
Polynomial(double* coefficient, int size);
~Polynomial() { delete [] polynomial ; }
double operator [] (int exponent) const;
friend ostream& operator << ( ostream& , const Polynomial&);
Polynomial& operator = (const Polynomial&);
double evaluate(double x) const;
int getSize() const;
double operator [] (int exponent, double coefficient);
Polynomial& operator + ( Polynomial& );
Polynomial& operator + ( double x );
Polynomial& operator - (Polynomial&);
Polynomial& operator - (double x);
Polynomial& operator * (Polynomial&);
Polynomial& operator * (double x);
private:
double* polynomial;
};
In this code, I want double operator [] (int exponent, double coefficient) to take an index (exponent) for the array and set the value at that index to the double coefficient value.
Your question seems to want two different things, the first bit is answered in the comments, return a reference. Here is an example for completeness,
struct example
{
double value_[10];
double& operator [] (int index) {
return value_[index];
}
};
int main() {
example e;
e[0] = 2.2;
std::cout << e.value_[0] << std::endl;
}
Demo
You then say..
I want double operator [] (int exponent, double coefficient) to take an index (exponent) for the array and set the value at that index to the double coefficient value.
You can't have operator[] with multiple arguments. A couple of options are;
accept a std::pair<int,double>, which has pretty clean syntax. for example..
struct example
{
double operator [] (const std::pair<int,double>& values) {
return 2.0;
}
};
int main() {
example e;
e[{1,2.2}];
//or
e[std::make_pair(1,2.2)];
}
Demo
Or if you really want the comma, you could make your own type for the exponent and overload the comma operator.
struct exponent
{
int exp_;
double coeff_;
exponent(int value) : exp_(value),coeff_(0){}
operator std::pair<int,double>() {
return std::make_pair(exp_,coeff_);
}
exponent& operator,(double coeff) {
coeff_ = coeff;
return *this;
}
};
struct example
{
double operator [] (const std::pair<int,double>& values) {
return 2.0;
}
};
int main() {
example e;
e[exponent(1), 3.3];
}
Demo
I'd personally go with something like the first option, or overload operator() instead.
I'm having trouble overloading the + operator, and I can't figure out what the cause is. The + operator returns a Polynomial (called C) but it returns it by value, where as the assignment operator is expecting a Polynomial object as a parameter passed by reference. To make C=A+B work, do I need to have a second assignment operator function that takes a Polynomial passed by value as an argument? Thanks!
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void line(int lines);
class Polynomial
{
private:
int degree;
int* coeffs;
public:
//constructors
Polynomial() {degree=0;coeffs=new int[1];}
Polynomial(int deg) {degree=deg;coeffs=new int[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
friend Polynomial operator+(Polynomial& A, Polynomial& B);
void operator =(Polynomial A);
};
int main()
{
Polynomial A(5);
Polynomial B(5);
A.Randomize(5);
B.Randomize(5);
A.Show(cout);
line(2);
B.Show(cout);
line(2);
Polynomial C(5);
C=A+B;
C.Show(cout);
return 0;
}
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+(Polynomial& A, Polynomial& B)
{
Polynomial C;
if (A.degree>=B.degree)
{
C=A;
for (int i=B.degree; i>=0; i--)
{
C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
}
C.Show(cout);
return C;
}
else
{
C=B;
for (int i=A.degree; i>=0; i--)
{
C.coeffs[i]=A.coeffs[i]+B.coeffs[i];
}
C.Show(cout);
return C;
}
}
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)
{
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 int[A.degree+1];
degree=A.degree;
for (int i=A.degree; i>=0; i--)
{
coeffs[i]=A.coeffs[i];
}
}
This issue here is you have mixed up the global(Outside class definitions) operator+ and the member class definition of operator+.
(static) Polynomial operator+(Polynomial& A, Polynomial& B); This operator is used globally, so outside of a class.
Inside the class you need to use the following signature.
Polynomial& operator+(const Polynomial& other);
Here is an example.
Polynomial p;
Polynomial q;
p = p + q;
The code for this if the operator is define in the class is:
p = p.operator+(q); //only needs one parameter.
The code for this if the operator is define globally is:
p = ::operator+(p, q); //needs both parameter
NOTE:
To use it as a non member function remove Polynomial operator+(Polynomial& A, Polynomial& B); from your definition Polynomial Polynomial::operator+(Polynomial& A, Polynomial& B){/**/} should be move above the main function and it now becomes:
static Polynomial operator+(Polynomial& A, Polynomial& B){/**/}
Normally you would overload operator+ in one of two ways:
Polynomial operator+(const Polynomial &other) const;
or as a non-member function,
Polynomial operator+(Polynomial a, const Polynomial &b);
You don't normally need the friend qualifier for the latter since the implementation will likely be in terms of another overloaded operator already defined in the class:
Polynomial operator+=(const Polynomial &other);
Then your non-member implementation will just be:
Polynomial operator+(Polynomial a, const Polynomial &b)
{
a+=b;
return a;
}
If you insist on using the function as defined in your code, then you will need to make it a
friend if you need access to private members:
Polynomial operator+(Polynomial &a, Polynomial &b)
{
Polynomial p;
// add them as needed
return p;
}
How can I modify the following code in such way I don't need to repeat f2=11;
f3=12; in the main function. The code is for overloading the most common operators.
class FLOAT{
private:
float x;
public:
FLOAT(){ x=0.0; }
void setFloat(float f) { x=f; }
float getFloat() { return x;};
FLOAT operator+(FLOAT obj) {x=x+obj.x; return *this;};
FLOAT operator-(FLOAT obj) {x=x-obj.x; return *this;};
FLOAT operator*(FLOAT obj) {x=x*obj.x; return *this;};
FLOAT operator/(FLOAT obj) {x=x/obj.x; return *this;};
FLOAT& operator=(const FLOAT& obj) {this->x=obj.x; return *this; };
FLOAT& operator=(const float& y) {this->x=y; return *this; };
};
int main() {
FLOAT f,f2,f3;
f2=11;
f3=12;
f=f3-f2;
cout<<"f3-f2 ="<<f.getFloat()<<endl;
f2=11;
f3=12;
f=f3+f2;
cout<<"f3+f2 ="<<f.getFloat()<<endl;
f2=11;
f3=12;
f=f3*f2;
cout<<"f3*f2 ="<<f.getFloat()<<endl;
f2=11;
f3=12;
f=f3/f2;
cout<<"f3/f2 ="<<f.getFloat()<<endl;
system("pause"); // to pause console screen
return 0;
}
#Oli's answer pretty much says you what minimal thing you need to do in order to make your code work. However, I see (and I know even #Oli sees) that your implementation of the class has many flaws.
Since you've implemented FLOAT, I'm explaining you the implementation of Double (the implementation of FLOAT would be similar).
class Double {
double data;
public:
Double (double p=0.0) : data(p){}
double value() { return data; }
Double & operator+=(Double const & other)
{
data += other.data;
return *this;
}
Double & operator-=(Double const & other)
{
data -= other.data;
return *this;
}
//...
};
Note that you don't need to implement operator=(Double const&) and Double(Double const&). The compiler generated ones would be enough. Since the constructor takes one argument of type double, you don't need to implement operator=(double const &) also. The compiler generated copy-semantics, along with the constructor, would take care of that.
Now see this,
//implement operator+ and operator- as non-member functions
Double operator+(Double a, Double const & b)
{
a += b; //a is local copy, so we can change it
return a;
}
Double operator-(Double a, Double const & b)
{
a -= b; //a is local copy, so we can change it
return a;
}
Note that I've implemented operator+ and operator- in terms of operator+= and operator-= respectively.
Similarly, you can implement operator/= and operator*= as member functions, and then implement operator/ and operator* in terms of the them!
Your operators should create a new instance; they shouldn't be modifying themselves (in fact, they should be declared const to prevent this).
e.g.:
FLOAT operator+(FLOAT obj) const
{
FLOAT tmp;
tmp.setFloat(x + obj.x);
return tmp;
}
Note there are much more idiomatic ways of defining operator overloads (e.g. defining operator+ in terms of operator+=, and defining a constructor that takes a float). But the above should suffice.
If I have the following files, I get this error (c2593 in VC9).
If I un-comment the prototype in main.cpp, the error disappears. I need to maintain the same functionality while keeping the class out of main.cpp. How can I do that?
Thanks.
main.cpp:
#include "number.h"
//const Number operator + (const Number & lhs, const Number & rhs);
int main(void)
{
Number n1(2); // n1 = 2
Number n2(9,3); // n2 = 3
Number n3 = n1+n2; // n3 = 5
}
number.h:
struct Number
{
int num;
Number(int n=0,int d=1) {num = n/d;}
operator int() {return num;}
operator double() {return num*1.0;}
};
number.cpp:
#include "Number.h"
const Number operator + (const Number & lhs, const Number & rhs)
{
Number tmp;
tmp.num = lhs.num + rhs.num;
return tmp;
}
Try putting the prototype in the Number header file:
number.h:
struct Number
{
int num;
Number(int n=0,int d=1) {num = n/d;}
operator int() {return num;}
operator double() {return num*1.0;}
};
const Number operator + (const Number & lhs, const Number & rhs);
number.cpp:
#include "Number.h"
const Number operator + (const Number & lhs, const Number & rhs)
{
Number tmp;
tmp.num = lhs.num + rhs.num;
return tmp;
}
main.cpp:
#include "number.h"
int main(void)
{
Number n1(2); // n1 = 2
Number n2(9,3); // n2 = 3
Number n3 = n1+n2; // n3 = 5
}
You never declare operator + in number.h, you only define it in number.cpp - therefore, when you include number.h in main.cpp, it doesn't know where to go to find operator +.
You must put the declaration of operator + in number.h, outside of the class, then define it in number.cpp
That commented line should go in number.h
EDIT: in number.h but as a free function.
One other thing to note with your code before you go back to us with a very similar question: Better remove the operator int and operator double functions. They will cause you major headache. Let's make a few examples:
Number a, b;
1 + b;
// ambiguous: operator+(int, int) or
// operator+(Number, Number) ?
// did you intend to use those for this case?
float x = a;
// ambiguous: from int -> float or
// double -> float ?
In the original situation you had, your addition was ambiguous, because there were operator+(double, double) and operator+(int, int) builtin operators considered and they were equally well. Others solved that problem. But before you start and run into these other problems, better remove the conversion functions and insert explicit functions like asDouble or something similar.
Apart from other's answers to declare operator+ in the header file, I suggest you to have operator+= in your struct as well.
struct Number
{
// your other declarations.
Number& operator+=(const Number& other)
{
this->num += other.num;
return *this;
}
};
const Number operator+(const Number& lhs, const Number& rhs)
{
Number ret(lhs);
ret += rhs;
return ret;
}
This way it is efficient to call x += y; instead of x = x + y;
Personally I like it better to declare the operators inside the class:
struct Number
{
int num;
Number(int n=0,int d=1) {num = n/d;}
operator int() {return num;}
operator double() {return num*1.0;}
Number operator+(const Number &arg) const;
};
and then:
Number Number::operator+(const Number &arg)
{
...
}
I'm wondering if you can overload an operator and use it without changing the object's original values.
Edited code example:
class Rational{
public:
Rational(double n, double d):numerator_(n), denominator_(d){};
Rational(){}; // default constructor
double numerator() const { return numerator_; } // accessor
double denominator() const { return denominator_; } // accessor
private:
double numerator_;
double denominator_;
};
const Rational operator+(const Rational& a, const Rational& b)
{
Rational tmp;
tmp.denominator_ = (a.denominator() * b.denominator());
tmp.numerator_ = (a.numerator() * b.denominator());
tmp.numerator_ += (b.numerator() * a.denominator());
return tmp;
}
I made the accessors const methods, but I'm still getting a privacy error for every tmp.denominator_ / numerator_.
Maybe I'm missing something, but why don't you just take out the code that modifies the arguments?
const Rational Rational::operator+(Rational& num)
{
Rational tmp;
tmp.denominator_ = (denominator_*num.denominator_);
//numerator_*=num.denominator_;
//num.numerator_*=denominator_;
tmp.numerator_ = (numerator_+num.numerator_);
return tmp;
}
This would be caught earlier by being const-correct.
That means your function signature should be this:
Rational Rational::operator+(const Rational& num) const
Then you will get errors because you are modifying const objects. The way your operators are written now is generally considered incorrect.
When you add 2 + 3, neither 2 nor 3 changes: they are const.
Edit
Sorry, I missed the actual math part. Here are a few things:
As a member function (what I have above), do this:
// Note that I use lhs and rhs to refer to the left-hand
// and right-hand sides of an operation. As a member function
// my left-hand side is implicitly `this`.
Rational Rational::operator+(const Rational& rhs) const
{
Rational temp;
temp.denominator_ = (denominator() * rhs.denominator());
temp.numerator_ = (numerator() * rhs.denominator());
temp.numerator_ += (denominator() * rhs.numerator());
return temp;
}
As a global function, do this:
Rational operator+(const Rational& lhs, const Rational& rhs)
{
Rational temp;
temp.denominator_ = (lhs.denominator() * rhs.denominator());
temp.numerator_ = (lhs.numerator() * rhs.denominator());
temp.numerator_ += (lhs.denominator() * rhs.numerator());
return temp;
}
The issue here is you'll get access violations: your member variables are private to the class. You need to let the compiler know that it is okay if this function handles your class's private variables by making the function a friend of the class:
class Rational
{
public:
friend Rational operator+(const Rational& lhs, const Rational& rhs);
}
What you're looking for are the "binary" addition and subtraction operators:
const Rational operator+(const Rational& A, const Rational& B)
{
Rational result;
...
return result;
}
update (in response to new code and comments):
You are getting that error because your accessor functions are not declared as constant functions, so the compiler has to assume that they might modify the original object. Change your accessors as follows, and you should be good to go:
double numerator() const { return numerator_; }
double denominator() const { return denominator_; }
update
To properly handle privacy issues, you should declare the binary operator+ function as a friend of the Rational class. Here is how it would look:
class Rational {
public:
Rational(double n, double d):numerator_(n), denominator_(d) {};
Rational() {}; // default constructor
double numerator() const { return numerator_; } // accessor
double denominator() const { return denominator_; } // accessor
friend Rational operator+(const Rational& A, const Rational& B);
private:
double numerator_;
double denominator_;
};
const Rational operator+(const Rational& a, const Rational& b)
{
Rational result;
result.denominator_ = (a.denominator_ * b.denominator_);
result.numerator_ = (a.numerator_ * b.denominator_);
result.numerator_ += (b.numerator_ * a.denominator_);
return result;
}
no .. you have to rewrite + and - .. it's not that hard. Then change the signatures to
const Rational Rational::operator+(Rational& num) const
const Rational Rational::operator-(Rational& num) const
that way the compiler will let you know if you are modifying your object.
Since your class already provides accessors to the numerator and denominator, and it has a public constructor, there is no need to use any friends. You can overload operator+ as follows:
const Rational operator+(const Rational& rhs, const Rational& lhs)
{
double newNumerator = rhs.numerator() * lhs.denominator() +
rhs.denominator() * lhs.numerator();
return Rational(newNumerator, rhs.denominator() * lhs.denominator());
}
In response to some of the other answers:
Everything about this question is exactly answered by Item 24 of Effective C++ (Third Edition). In this item, Scott Meyers shows that, in cases dealing with numerical types where you want to support implicit conversion in an intuitive manner, it is best to use a non-member non-friend function.
Say your Rational class had the following constructor:
Rational::Rational(double numerator = 0, double denominator = 1);
In this case, if operator+ were a member function, trying to do mixed mode arithmetic would work only half the time:
Rational oneHalf(1, 2);
oneHalf + 2; // works
2 + oneHalf; // error!
In the second example, operator+ for integers is called. The way to fix this is to make operator+ for Rationals a non-member function as shown above.
If you have access to Effective C++, you should also check out Item 23: Prefer non-member non-friend functions to member functions.
I prefer to implement operator+ in terms of operator+=. Then clients get to choose if they want a temp copy or not. Also operator+= is more natural as a member function so it has access to the private parts.
class Rational {
public:
Rational(double n, double d):numerator_(n), denominator_(d) {};
Rational() {}; // default constructor
double numerator() const { return numerator_; } // accessor
double denominator() const { return denominator_; } // accessor
Rational & operator+=(const Rational& b) {
denominator_ *= b.denominator_);
numerator_ *= (b.denominator_);
numerator_ += (b.numerator_ * denominator_);
return *this;
}
private:
double numerator_;
double denominator_;
};
const Rational operator+(const Rational& a, const Rational& b)
{
Rational result(a);
result += b;
return result;
}
However the easiest way to implement operator overloading is to use the boost operators library. Then you implement the minimum set of operators and boost take care of the rest and all the gotchas (including wintermute's example of 2 + oneHalf.