I'm trying to create an overload of the "+" within my program to add a private constant value to an already establish polynomial, but the last portion of my code comes out with the following error:

Error	1	error C2677: binary '+=' : no global operator found which takes type 'Polynomial' (or there is no acceptable conversion)	h:\documents\baker school\visual basic\polynomialmagic\polynomialclass.cpp	131	PolynomialMagic

I've tried switching out "+=" for "+" and I get the same error...what am I doing wrong?

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <cmath>
using namespace std;

class Polynomial
{
public:
	void Polynomial1 ();
	void Polynomial2 ();
	int Degree;
	int *coef1, *coef2;
	int *power1, *power2;
	int SoloNumber1;

private:
	static const int ConstValue = 5;
};

int operator +(Polynomial &Poly1, Polynomial ConstValue);

int main()
{
	int PolyChoice;

	Polynomial Poly1,Poly2;

	cout << "How many Polynomials Do You Want to Use: 1 or 2? ";
	cin >> PolyChoice;

	if (PolyChoice == 1)
	{
		Poly1.Polynomial1();
		cout << endl;
	}
	
	if (PolyChoice == 2)
	{		
		Poly1.Polynomial1();
		cout << endl;
		Poly2.Polynomial2();
	}

	return 0;
}

void Polynomial::Polynomial1()
{
	int i,n;

	cout << "What Degree Polynomial: ";
	cin >> i;

	coef1 = new (nothrow) int[i];
	power1 = new (nothrow) int[i];

	for (n=0; n<(i); n++)
	{
		cout << "Enter Coefficient #: ";
		cin >> coef1[n];
	}

	for (n=0;n<=(i);n++)
	{
		power1[n]= n+1;
	}


	cout << "Enter Solo Numbers (Those Without A Corresponding x): ";
	cin >> SoloNumber1;

	cout << "Polynomial selected is: ";

	for (n=0;n<i;n++)
	{
		if(n != (i-1))
			cout << coef1[n] << "x^(" << power1[n] << ")" << " + ";
		else
			cout << coef1[n] << "x^(" << power1[n] << ") + " << SoloNumber1;
	}
}

void Polynomial::Polynomial2()
{
	int h,j, SoloNumber2;

	cout << "What Degree Polynomial: ";
	cin >> h;

	coef2 = new (nothrow) int[h];
	power2 = new (nothrow) int[h];
	

	for (j=0; j<h; j++)
	{
		cout << "Enter Coefficient #: ";
		cin >> coef2[j];
	}

	for (j=0;j<h;j++)
	{
		power2[j]= j;
	}

	cout << "Enter Solo Numbers (Those Without A Corresponding x): ";
	cin >> SoloNumber2;


	cout << "Polynomial selected is: ";

	for (j=0;j<h;j++)
	{
		if(j != (h-1))
			cout << coef2[j] << "x^(" << power2[j] << ")" << "+";
		else
			cout << coef2[j] << "x^(" << power2[j] << ") + " << SoloNumber2 << endl;
	}
}

int operator +(Polynomial Poly1, Polynomial &ConstValue)
{
	int NewValue;
	
	NewValue += Poly1.SoloNumber1;
	NewValue += ConstValue;

	return NewValue;
}

Edited 6 Years Ago by PDB1982: n/a

I was trying this out, but now it's saying that it can't access the private member declared in Polynomial:

int operator +(Polynomial Polynomial1, Polynomial ConstValue)
{
	int NewValue=0;
	
	NewValue += Polynomial1.SoloNumber1;
	NewValue += Polynomial1.ConstValue;

	return NewValue;
}

So, I figured out that I needed to make my private const variable into a public const variable, but how do I insert it into main?

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <cmath>
using namespace std;

class Polynomial
{
public:
	void Polynomial1 ();
	void Polynomial2 ();
	int Degree;
	int *coef1, *coef2;
	int *power1, *power2;
	int SoloNumber1;
	static const int ConstValue = 5;

private:
};

int operator +(Polynomial &Poly1, Polynomial &ConstValue);

int main()
{
	int PolyChoice;

	Polynomial Poly1,Poly2;

	cout << "How many Polynomials Do You Want to Use: 1 or 2? ";
	cin >> PolyChoice;

	if (PolyChoice == 1)
	{
		Poly1.Polynomial1();
		cout << endl;
	}
	
	if (PolyChoice == 2)
	{		
		Poly1.Polynomial1();
		cout << endl;
		Poly2.Polynomial2();
	}

	return 0;
}

void Polynomial::Polynomial1()
{
	int i,n;

	cout << "What Degree Polynomial: ";
	cin >> i;

	coef1 = new (nothrow) int[i];
	power1 = new (nothrow) int[i];

	for (n=0; n<(i); n++)
	{
		cout << "Enter Coefficient #: ";
		cin >> coef1[n];
	}

	for (n=0;n<=(i);n++)
	{
		power1[n]= n+1;
	}


	cout << "Enter Solo Numbers (Those Without A Corresponding x): ";
	cin >> SoloNumber1;

	cout << "Polynomial selected is: ";

	for (n=0;n<i;n++)
	{
		if(n != (i-1))
			cout << coef1[n] << "x^(" << power1[n] << ")" << " + ";
		else
			cout << coef1[n] << "x^(" << power1[n] << ") + " << SoloNumber1;
	}
}

void Polynomial::Polynomial2()
{
	int h,j, SoloNumber2;

	cout << "What Degree Polynomial: ";
	cin >> h;

	coef2 = new (nothrow) int[h];
	power2 = new (nothrow) int[h];
	

	for (j=0; j<h; j++)
	{
		cout << "Enter Coefficient #: ";
		cin >> coef2[j];
	}

	for (j=0;j<h;j++)
	{
		power2[j]= j;
	}

	cout << "Enter Solo Numbers (Those Without A Corresponding x): ";
	cin >> SoloNumber2;


	cout << "Polynomial selected is: ";

	for (j=0;j<h;j++)
	{
		if(j != (h-1))
			cout << coef2[j] << "x^(" << power2[j] << ")" << "+";
		else
			cout << coef2[j] << "x^(" << power2[j] << ") + " << SoloNumber2 << endl;
	}
}

int operator +(Polynomial Polynomial1, Polynomial ConstValue)
{
	int NewValue=0;
	
	NewValue += Polynomial1.SoloNumber1;
	NewValue += Polynomial1.ConstValue;

	return NewValue;
}

Edited 6 Years Ago by PDB1982: n/a

So, I figured out that I needed to make my private const variable into a public const variable, but how do I insert it into main?

What does "insert it into main" mean?

The function prototype :

int operator +(Polynomial &Poly1, Polynomial &ConstValue);

The function definition header:

int operator +(Polynomial Polynomial1, Polynomial ConstValue)

What does "insert it into main" mean?

Well, if I figured out what to do, in order to properly compile the program, but it doesn't output anything. I'm just beginning to learn this part, and I'm not sure what to do with it from here. The overloaded "operator +" will return a value, but how do I get my program to output that value?

For instance, I have:

int main()
{
	int PolyChoice;

	Polynomial Poly1,Poly2;

	cout << "How many Polynomials Do You Want to Use: 1 or 2? ";
	cin >> PolyChoice;

	if (PolyChoice == 1)
	{
		Poly1.Polynomial1();
		cout << endl;
	}
	
	if (PolyChoice == 2)
	{		
		Poly1.Polynomial1();
		cout << endl;
		Poly2.Polynomial2();
	}

	return 0;
}

in which the Poly1.Polynomial1() function is used to output the values input into the program by the user to create their first polynomial. Let's say that I want polynomial1 to be added to a const variable, which was accomplished in "operator +", what do I have to put into my main to reflect that addition process?

Edited 6 Years Ago by PDB1982: n/a

Please don't post too much code - it is difficult for people to understand it at a glance, split it by parts and ask the questions to each part of the code.

Usually to override global operator+() function a programmer does something like

Type operator+(const Type &t, const Type &t)

'const' keyword here is much likely to exist.

Why do you want to return 'int' as a result of adding two classes of type 'Polynomial' ? It is quite unusual to do such things.
Also, where is the place in your code where this function is called?

This article has been dead for over six months. Start a new discussion instead.