I keep getting the following error:

Error	1	error C2678: binary '+' : no operator found which takes a left-hand operand of type 'int' (or there is no acceptable conversion)

and I can't figure out how to fix it...I believe it has something to do with the red portion of the code, but I can't seem to fix it...Any Suggestions....

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

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

private:
};

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

int main()
{
	int PolyChoice,Poly1PlusConst;

	Polynomial Poly1,Poly2,Poly3;

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

	while (PolyChoice)
	{
		if (PolyChoice == 1)
		{
			Poly3.Polynomial3(Poly1);
			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++)
	{
		cout << "Enter Power For Coefficient ";
		cin >> power1[n];
	}


	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] << ") + ";
	}
	cout << SoloNumber1;
}

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

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

	coef2 = new int[h];
	power2 = new int[h];
	

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

	for (j=0;j<h;j++)
	{
		cout << "Enter First Power: ";
		cin >> power2[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;
	}
}

void Polynomial::Polynomial3(Polynomial &ConstValue)
{
	int i,n,Poly1PlusConst;

	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++)
	{
		cout << "Enter Power For Coefficient ";
		cin >> power1[n];
	}


	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] << ") + ";
	}
	Poly1PlusConst=SoloNumber1 + ConstValue;
	cout << Poly1PlusConst << endl;
}

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

	return NewValue;
}

Edited 6 Years Ago by PDB1982: n/a

You didn't define a operator+, that takes an int. You only defined
a operator+ that takes another Polynomial.

You didn't define a operator+, that takes an int. You only defined
a operator+ that takes another Polynomial.

How do I set that up, if the SoloNumber1 variable is established within Polynomial.Poly3?

Would it be

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

	return NewValue;
}

How do I set that up, if the SoloNumber1 variable is established within Polynomial.Poly3?

Would it be

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

	return NewValue;
}

I've tried everything that I can think of, but I'm still getting that error....any suggestions?

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