In: Computer Science
I working on this program in C++ and I keep getting 20 errors of the same type
again.cpp:36:11: error: use of undeclared identifier 'Polynomial'
int main() {
// create a list of polinomials
vector<Polynomial> polynomials;
// welcome message
cout << "Welcome to Polynomial Calculator" <<
endl;
int option = 0;
while (option != 6) {
// display menu
displayMenu();
// get user input;
cin >> option;
if (option == 1) {
cout << "Enter a polynomial :" << endl;
string str = "";
cin.ignore();
getline(cin, str);
Polynomial p(str);
polynomials.push_back(p);
}
else if (option == 2) {
cout << "List of polynomials: " << endl;
for (int i = 0; i < polynomials.size(); i++) {
cout << (i+1) << ". " <<
polynomials.at(i).toString() << endl;
}
}
else if (option == 3) {
int first = 0;
int second = 0;
cout << "select first polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++) {
cout << (i + 1) << ". " <<
polynomials.at(i).toString() << endl;
}
cin >> first;
cout << "select second polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++) {
cout << (i + 1) << ". " <<
polynomials.at(i).toString() << endl;
}
cin >> second;
Polynomial result = polynomials.at(first -
1).sum(polynomials.at(first - 1), polynomials.at(second -
1));
cout << "Sum is: " << result.toString() <<
endl;
}
else if (option == 4) {
int first = 0;
int second = 0;
cout << "select first polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++) {
cout << (i + 1) << ". " <<
polynomials.at(i).toString() << endl;
}
cin >> first;
cout << "select second polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++) {
cout << (i + 1) << ". " <<
polynomials.at(i).toString() << endl;
}
cin >> second;
Polynomial result = polynomials.at(first -
1).product(polynomials.at(first - 1), polynomials.at(second -
1));
cout << "Product is: " << result.toString() <<
endl;
}
else if (option == 5) {
int x;
int select;
cout << "select a polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++) {
cout << (i + 1) << ". " <<
polynomials.at(i).toString() << endl;
}
cin >> select;
cout << "Enter value of x: ";
cin >> x;
cout << "Result is: " << polynomials.at(select -
1).evaluate(x) << endl;
}
else if (option == 6) {
break;
}
else {
// print error message
cout << "Invalid Input!" << endl;
}
}
return 0;
}
class Polynomial {
private:
vector<Term> poly; // list of terms
public:
Polynomial(); // constructor
Polynomial(string init); // constructor with parameter
void addTerm(Term term); // add a term to the polynomial
double evaluate(int x); // return value of the expression for given
x
Polynomial& sum(Polynomial& a, Polynomial& b); //
produces new polynomial which is the sum of the two argument
polynomials
Polynomial& product(Polynomial& a, Polynomial& b); //
produces new polynomial which is the product of the two argument
polynomials
string toString(); // returns string representation of the
polynomial
// suporting functions needed
Term& getTerm(int x); // return term at index i from the
list
int getOrderofPolynomial(); // returns order of polynomial
};
Polynomial::Polynomial() {
// create a empty polinomial
}
Polynomial::Polynomial(string init) {
// create a stringstream object to read init data
stringstream ss(init);
int coef;
int expo;
while (!ss.eof()) {
// create a term
ss >> coef;
ss >> expo;
Term t(coef, expo);
// add term to polinomial
addTerm(t);
}
}
The ERROR:
again.cpp:36:11: error: use of undeclared identifier 'Polynomial'
vector<Polynomial> polynomials;
You need to declare the class before main()
I've done that for you here, but you've omitted the definition of Term. If it works with just this change, let me know, otherwise maybe you can add the defintion for Term.
#include<vector>
#include<iostream>
using namespace std;
class Polynomial
{
private:
vector<Term> poly; // list of terms
public:
Polynomial(); // constructor
Polynomial(string init); // constructor with parameter
void addTerm(Term term); // add a term to the polynomial
double evaluate(int x); // return value of the expression for given x
Polynomial& sum(Polynomial& a, Polynomial& b); // produces new polynomial which is the sum of the two argument polynomials
Polynomial& product(Polynomial& a, Polynomial& b); // produces new polynomial which is the product of the two argument polynomials
string toString(); // returns string representation of the polynomial
// suporting functions needed
Term& getTerm(int x); // return term at index i from the list
int getOrderofPolynomial(); // returns order of polynomial
};
Polynomial::Polynomial()
{
// create a empty polinomial
}
Polynomial::Polynomial(string init)
{
// create a stringstream object to read init data
stringstream ss(init);
int coef;
int expo;
while (!ss.eof()) {
// create a term
ss >> coef;
ss >> expo;
Term t(coef, expo);
// add term to polinomial
addTerm(t);
}
};
int main()
{
// create a list of polinomials
vector<Polynomial> polynomials;
// welcome message
cout << "Welcome to Polynomial Calculator" << endl;
int option = 0;
while (option != 6)
{
// display menu
displayMenu();
// get user input;
cin >> option;
if (option == 1)
{
cout << "Enter a polynomial :" << endl;
string str = "";
cin.ignore();
getline(cin, str);
Polynomial p(str);
polynomials.push_back(p);
}
else if (option == 2)
{
cout << "List of polynomials: " << endl;
for (int i = 0; i < polynomials.size(); i++)
cout << (i+1) << ". " << polynomials.at(i).toString() << endl;
}
else if (option == 3)
{
int first = 0;
int second = 0;
cout << "select first polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++)
cout << (i + 1) << ". " << polynomials.at(i).toString() << endl;
cin >> first;
cout << "select second polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++)
cout << (i + 1) << ". " << polynomials.at(i).toString() << endl;
cin >> second;
Polynomial result = polynomials.at(first - 1).sum(polynomials.at(first - 1), polynomials.at(second - 1));
cout << "Sum is: " << result.toString() << endl;
}
else if (option == 4)
{
int first = 0;
int second = 0;
cout << "select first polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++)
cout << (i + 1) << ". " << polynomials.at(i).toString() << endl;
cin >> first;
cout << "select second polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++)
cout << (i + 1) << ". " << polynomials.at(i).toString() << endl;
cin >> second;
Polynomial result = polynomials.at(first - 1).product(polynomials.at(first - 1), polynomials.at(second - 1));
cout << "Product is: " << result.toString() << endl;
}
else if (option == 5)
{
int x;
int select;
cout << "select a polinomial: " << endl;
for (int i = 0; i < polynomials.size(); i++)
cout << (i + 1) << ". " << polynomials.at(i).toString() << endl;
cin >> select;
cout << "Enter value of x: ";
cin >> x;
cout << "Result is: " << polynomials.at(select - 1).evaluate(x) << endl;
}
else if (option == 6)
break;
else // print error message
cout << "Invalid Input!" << endl;
}
return 0;
}