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...
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 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 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...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
I am making a html game with phaser 3 I keep getting an error at line...
I am making a html game with phaser 3 I keep getting an error at line 53 of this code (expected ;) I have marked the line that needs to be fixed. whenever I add ; my whole code crashes const { Phaser } = require("./phaser.min"); var game; var gameOptions = {     tileSize: 200,     tileSpacing: 20,     boardSize: {     rows: 4,     cols: 4     }    }    window.onload = function() {     var gameConfig = {         width: gameOptions.boardSize.cols * (gameOptions.tileSize +             gameOptions.tileSpacing) + gameOptions.tileSpacing,...
I am working on a C++ program, where a user puts in a notation a playing...
I am working on a C++ program, where a user puts in a notation a playing card and the output is the full name of the card.(ex: KH = King of Hearts) I have most of it working but I want to have an error come up when the user puts in the wrong info and then loop back to the original question. Am I setting it up wrong? Pasted below is my code #include<iostream> #include<string> using namespace std; int...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in...
My code works in eclipse, but not in Zybooks. I keep getting this error. Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1478) at Main.main(Main.java:34) Your output Welcome to the food festival! Would you like to place an order? Expected output This test case should produce no output in java import java.util.Scanner; public class Main {    public static void display(String menu[])    {        for(int i=0; i<menu.length; i++)        {            System.out.println (i + " - " + menu[i]);...
For this discussion, comment on any programming errors you encountered. Did the same issues keep recurring?
For this discussion, comment on any programming errors you encountered. Did the same issues keep recurring?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT