Question

In: Computer Science

I working on this program in C++ and I keep getting 20 errors of the same...

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;

Solutions

Expert Solution

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;
}


Related Solutions

My program is working until it gets to val2 / 1000000. I keep getting my answer...
My program is working until it gets to val2 / 1000000. I keep getting my answer to be 0. Can someone help please? int main() {    int n, num, num1, num2, time, val1, val2, Ts;                printf("**** Welcome to the Time Study Program ****\n");    printf("\n");    printf("Enter the number of elements for the give operation being perform\n");    printf("Number of elements: ");    scanf_s("%d", &n);    printf("\n");    printf("Enter the Performance Rating (PR) factor...
C++ Programming-Need getGrades function (I keep getting errors). Write a function called getGrades that does the...
C++ Programming-Need getGrades function (I keep getting errors). Write a function called getGrades that does the following: Take an array of integers and an integer representing the size of the array as parameters. Prompt the user to enter up to 20 grades. Store the grades in the array. Return the number of grades entered. Write another function called calcStats that does the following: Take an array of integers and an integer representing the size of the array as normal parameters....
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I keep getting minor errors I can't figure out and I don't know how to convert...
I keep getting minor errors I can't figure out and I don't know how to convert decimal .10 to percentage 10% either.   With these functions defined now expand the program for a company who gives discounts on items bought in bulk. Create a main function and inside of it ask the user how many different items they are buying. For each item have the user input a price and quantity, validating them with the functions that you wrote. Use your...
(C++) I'm getting a few errors that prevents the program from running what is causing this?...
(C++) I'm getting a few errors that prevents the program from running what is causing this? " routes3.cpp:202:9: warning: add explicit braces to avoid dangling else [-Wdangling-else] else { ^ " #include <iostream> #include <vector> #include <string> #include <set> #include <cstring> using namespace std; class Leg{ const char* const startCity; const char* const endCity; const int dist; public: Leg(const char* const, const char* const, int); Leg& operator= (const Leg&); int getDist() const {return dist;} void output(ostream&) const; friend class Route;...
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint....
I keep getting the same error Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'test_ibfk_5' in the referenced table 'appointment', can you please tell me what is wrong with my code: -- Table III: Appointment = (site_name [fk7], date, time) -- fk7: site_name -> Site.site_name DROP TABLE IF EXISTS appointment; CREATE TABLE appointment (    appt_site VARCHAR(100) NOT NULL, appt_date DATE NOT NULL, appt_time TIME NOT NULL, PRIMARY KEY (appt_date, appt_time), FOREIGN KEY (appt_site)...
I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that...
I created a shoppingcart program but I'm getting a few compilation failed errors: 1) Tests that ItemToPurchase("Bottled Water", "Deer Park, 12 oz.", 1, 10) correctly initializes item compilation failed 2) Tests default constructor and accessors for ShoppingCart Compilation failed 3)Tests ShoppingCart("John Doe", "February 1, 2016") correctly initializes cart Compilation failed 4) Tests that getNumItemsInCart() returns 6 (ShoppingCart) compilation failed 5) Test that getCostOfCart() returns 9 (ShoppingCart) compilation failed Complete program: itemtopurchase.java public class ItemToPurchase { // instance variables private String...
c++ I am getting errors that say "expected a declaration". can you explain what it means...
c++ I am getting errors that say "expected a declaration". can you explain what it means by this and how I need to fix it?   #include <iostream> using namespace std; //functions void setToZero(int board[8][8]); void displayBoard(int oboard[8][8]); void movingFunction(); int main() {    // chess board 8x8    int board[8][8];    // all of the possible moves of the knight in 2 arrays    int colMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };    int rowMove[8]...
Hello, I keep getting the error C4430: missing type specifier - int assumed. Note: C++ does...
Hello, I keep getting the error C4430: missing type specifier - int assumed. Note: C++ does not support default-int for my code. What am I doing wrong? I only have #include <iostream> int read_num(void); main() {    int num1, num2, max;    //find maximum of two 2 numbers    num1 = read_num();    num2 = read_num();    if (num1 > num2)    {        max = num1;    }    else    {        max = num2;   ...
I did most of this problem theres only a part that I keep getting wrong or...
I did most of this problem theres only a part that I keep getting wrong or not getting. I need help figuring out the blank boxes. that says Purchasing and Power and Question 2. Rest is done Sequential Method Jasmine Company manufactures both pesticide and liquid fertilizer, with each product manufactured in separate departments. Three support departments support the production departments: Power, General Factory, and Purchasing. Budgeted data on the five departments are as follows: Support Departments Producing Departments Power...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT