Why is my constructor not working in my operator overload?

Hello everyone! I am programming a polynomial class and am overloading the operators (+,-,*). I have my + operator coded I think. Except in my return statement it says the constructor does not exist. What am I doing wrong here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class polynomial
{
public:
	polynomial(int degree_Size, int coefficient[]);
	polynomial(int coefficient[]);
	polynomial();//sets degreeSize to 10
	polynomial(const polynomial& polynomailObject);//copy constructor
	polynomial& operator = (const polynomial& rightSide);
	~polynomial();
	friend const polynomial operator +(const polynomial& number1, const polynomial& number2);
	friend const polynomial operator -(const polynomial& number1, const polynomial& number2);
	friend const polynomial operator *(const polynomial& number1, const polynomial& number2);
	friend istream& operator >> (istream& inputStream, polynomial& newNumber);
	friend ostream& operator <<(ostream& outputStream, const polynomial& number);
	int getDegreeSize()const { return degreeSize; }
	int degreeSize;
	int *privateCoefficient;

};
  const polynomial operator +(const polynomial& number1, const polynomial& number2)
{
	int number1size, number2size;
	number1size = number1.degreeSize;
	number2size = number2.degreeSize;
	if (number1size > number2size)
	{
		IntPtr a;
		a = new int[number1.degreeSize];
		for (int i = 0; i <= number1size; i++)
		{
			return polynomial((number1.degreeSize), (a[number1.privateCoefficient[i]+number2.privateCoefficient[i]]));
		}
	}
}
Hello OP, your (multiple!) posts on this topic intrigued me and so I tried to come up with a solution. I'm afraid it's a bit different to what you may have done so far. However if you give this a shot you might find that it's not too hard to understand and should be doing most (all?) of what you want. I've also put comments throughout the program.
the key change I"ve made compared to yours is to have a struct Term that captures the co-efficients and exponents of the polynomial and each PolyNom object then has a std::set<Term> as a data member with the exponents being the key - this helps to organize the terms of the polynomial in one container facilitating the use of the standard library facilities for various operations:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <iostream>
#include <set>
#include <algorithm>

struct Term
{
    size_t m_exponent;
    double m_coefficient;
    Term(const size_t exponent, const double coefficient) :
        m_exponent(exponent), m_coefficient(coefficient){}
};
struct TermCompare//to use struct Term in std::set<Term>
{
    bool operator ()(const Term& lhs, const Term& rhs)
    {
        return lhs.m_exponent < rhs.m_exponent;
    }
};

using Order = std::set<Term, TermCompare>;//custom comparator for the set is TermCompare

struct PolyNom
{
    Order m_factors;
    PolyNom(){}
    PolyNom(const Order& factors): m_factors(factors){}
    double evaluate(const double& value)const;//evaluates value of polynomial expression for any double
    PolyNom operator + ( PolyNom& rhs);
    PolyNom operator - (const PolyNom& rhs);//implementation left as an exercise
    PolyNom operator * (const PolyNom& rhs);
};
std::ostream& operator << (std::ostream& os, const PolyNom& p)//for printing
{
    auto itr = p.m_factors.cbegin();
    for (; itr != --p.m_factors.cend(); ++itr)
    {
        os << (*itr).m_coefficient << "X^" << (*itr).m_exponent << " + ";
    }
    for (; itr != p.m_factors.cend(); ++itr)//so that there is no + sign after the last term
    {
        os << (*itr).m_coefficient << "X^" << (*itr).m_exponent << "\n";
    }
    return os;
}

int main()
{
    std::set<Term, TermCompare> a {{1, 2}, {3, 4.5}, {0,6}};
    std::set<Term, TermCompare> b{{0,3}, {3,8}, {14, 6}, {9, 2}};
    PolyNom p{a}, q{b};
    std::cout << p << "\n";
    std::cout << q << "\n";
    std::cout << p.evaluate(7.5) << "\n";
    auto pq = p * q;
    std::cout << pq << "\n";
}
double PolyNom::evaluate(const double& value)const
{
    double sum {};
    for (const auto& elem: m_factors)
    {
        sum += (elem.m_coefficient) * std::pow(value, (elem.m_exponent));
    }
    return sum;
}
 PolyNom PolyNom::operator + ( PolyNom& rhs)
 {
    PolyNom result{};
    result.m_factors = m_factors;//first assign the terms of this object to terms of the result...
    for ( auto elem : rhs.m_factors)
    {
        if(!result.m_factors.insert(elem).second)//... then if a particular term of the rhs cannot be inserted into result terms ...
                //... it implies that exponent is already present in results ...
        {
           auto itr = std::find_if(result.m_factors.begin(), result.m_factors.end(), [&](const Term& t)
                        {return elem.m_exponent == t.m_exponent; }); // ... so find the term in results with the required exponent ...
            auto temp = (*itr).m_coefficient; // ... save its coefficient
            temp += elem.m_coefficient; // ... add in the coefficient from the rhs
            result.m_factors.erase(itr); // now remove that term from the result's set of terms ...
            result.m_factors.insert(Term(elem.m_exponent, temp)); // ... insert new term that has sum of coefficients of this and rhs
        }
    }
    return result;
 }
 PolyNom PolyNom::operator * (const PolyNom& rhs)
 {
     PolyNom result{};
     auto maxExponent = (*m_factors.crbegin()).m_exponent + (*rhs.m_factors.crbegin()).m_exponent;//max exponent
     auto minExponent = (*m_factors.cbegin()).m_exponent + (*rhs.m_factors.cbegin()).m_exponent;//min exponent
     for (auto i = minExponent; i <= maxExponent; ++i)// all other exponents from multiplication must fall b/w max and min exponent
     {
         double tempCoefficient{};
         for (auto itr_this = m_factors.cbegin(); itr_this != m_factors.cend(); ++itr_this)
         {
             for (auto itr_rhs = rhs.m_factors.cbegin(); itr_rhs != rhs.m_factors.cend(); ++itr_rhs)
             {

                 if((*itr_this).m_exponent + (*itr_rhs).m_exponent == i)// if we find a matching exponent ...
                 {
                    tempCoefficient += (*itr_this).m_coefficient + (*itr_rhs).m_coefficient;//... then record sum of corresponding co-efficients
                 }
             }
         }
         if(tempCoefficient != 0)//i.e. some actual terms exists
         {
             result.m_factors.insert(Term(i, tempCoefficient));//then store the term in result
             tempCoefficient = 0;
         }
    }
    return result;
 }
Hi! Thank you! No this is great! I will definitely take some time later on to look at this program. However, I feel like this is a little beyond my skills, you're using some things that I never seen before. However, I made progress on my approach and I'd really like some outside perspective. Tell you what, you take a look at my program- I will turn your program back into you and complete the - operator hahaha? Fair trade? If not, it's totally OK. I just want to keep using this one because I finally got the program to add a little bit. It skips the first term and gives me a memory address instead I presume. Here is the program so you can run it in visual studios if you want. If you don't feel like helping anymore, its totally fine! Sorry about the multiple posts by the way, I had trouble figuring out which thread I should use and then I needed to update my post because my code had changed! Thank you!
My Program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<iostream>
#include<cstdlib>
using namespace std;

typedef int* IntPtr;
class polynomial
{
public:
	polynomial(int degree_Size, int coefficient[]);
	polynomial();//sets degreeSize to 10
	polynomial(const polynomial& polynomialObject);//copy constructor
	polynomial& operator = (const polynomial& rightSide);
	~polynomial();
	friend const polynomial operator +(const polynomial& number1, const polynomial& number2);
	friend const polynomial operator -(const polynomial& number1, const polynomial& number2);
	friend const polynomial operator *(const polynomial& number1, const polynomial& number2);
	friend istream& operator >> (istream& inputStream, polynomial& newNumber);
	friend ostream& operator <<(ostream& outputStream, const polynomial& number);
	int getDegreeSize()const { return degreeSize; }
	int degreeSize;
	int *privateCoefficient;

};
polynomial::polynomial(int degree_Size, int coefficient[])
{
	degreeSize = degree_Size;
	privateCoefficient = new int[degreeSize];
	for (int i = 0; i <= degreeSize; i++)
	{
		privateCoefficient[i] = coefficient[i];
	}
}
polynomial::polynomial()
{
	degreeSize = 10;
	privateCoefficient = new int[degreeSize];
}
polynomial::~polynomial()
{
	if (privateCoefficient)
	{
		delete[] privateCoefficient;
		privateCoefficient = NULL;
	}
}
polynomial::polynomial(const polynomial& polynomialObject) :degreeSize(polynomialObject.getDegreeSize())
{
	privateCoefficient = new int[degreeSize];
	for (int i = 0; i < degreeSize; i++)
	{
		privateCoefficient[i] = polynomialObject.privateCoefficient[i];
	}
}
polynomial& polynomial::operator = (const polynomial& rightSide) {
	if (this == &rightSide)
	{
		return *this;
	}
	else
	{
		degreeSize = rightSide.degreeSize;
		delete[] privateCoefficient;
		privateCoefficient = new int[degreeSize];
		for (int i = 0; i < degreeSize; i++)
		{
			privateCoefficient[i] = rightSide.privateCoefficient[i];
		}
		return *this;
	}
}
istream& operator >> (istream& inputStream, polynomial& newNumber)
{
	cout << "Please enter the highest degree of your polynomial: X^";
	inputStream >> newNumber.degreeSize;
	IntPtr a;
	a = new int[newNumber.degreeSize];
	for (int i = 0; i <= newNumber.degreeSize; i++)
	{
		cout << "Please enter the X^" << i << " term coefficient: " << endl;
		inputStream >> newNumber.privateCoefficient[i];
	}
	return inputStream;
}
ostream& operator << (ostream& outputStream, const polynomial& newNumber)
{
	for (int i = newNumber.degreeSize; i >= 0; i--)
	{
		outputStream << newNumber.privateCoefficient[i] << "X^" << i<<" ";
	}
	return outputStream;
}
const polynomial operator +(const polynomial& number1, const polynomial& number2)
{
	int number1size, number2size;
	number1size = number1.degreeSize;
	number2size = number2.degreeSize;
	if (number1size >= number2size)
	{
		IntPtr a;
		a = new int[number1.degreeSize];
		for (int i = 0; i < number1size; i++)
		{
			a[i] = number1.privateCoefficient[i] + number2.privateCoefficient[i];
		}
		polynomial newPoly(number1.degreeSize, a);
		return newPoly;
	}
}
int main()
{
	polynomial number1, number2, number3,number4;
	cin >> number1;
	cin >> number2;
	cout << "Houston we are attempting to add: ";
	number4 = number1 + number2;
	cout << number4;
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.