0

I have been having a lot of issues with header files, and now it seems that the vector that is declared in my header file, Polynomial.hpp, is not being recognized in Polynomial.cpp. I have already included std:: which seems to be a common mistake, so I don't know where to go from here.

Header file:

#ifndef POLYNOMIAL_HPP #define POLYNOMIAL_HPP #include<vector> #include"term.hpp" class Polynomial { private: std::vector<Term> vect; public: Polynomial(); ~Polynomial(); void add(Term t); void print(); Polynomial combineLikeTerms(); }; #endif 

cpp File:

#include "term.hpp" #include "Polynomial.hpp" #include<iostream> #include<map> using namespace std; void add(Term t) { vect.push_back(t); } void print() { for(int i = 0; i < vect.size(); i++) { cout << vect[i].toString(); } } Polynomial combineLikeTerms() { Polynomial poly; map<int, int> combinedPoly; for(int j = 0; j < vect.size(); j++) { combinedPoly.insert(pair<int, int>(vect[j].getExponent(), vect[j].getCoefficient()); } for(map<int,int>::iterator itr = combinedPoly.begin(); itr != combinedPoly.end(); itr++) { Term newTerm(itr->second, "x", itr->first); poly.add(newTerm); } return poly; } 

Error (1/6):

Polynomial.cpp:9:5: error: use of undeclared identifier 'vect' vect.push_back(t);

0

4 Answers 4

2

In Polynomial.cpp you are defining new functions instead of member functions. Change the definitions to use the class name like

void Polynomial::add(Term t) { vect.push_back(t); } 
Sign up to request clarification or add additional context in comments.

Comments

1

Your void add(Term T) in Polynomial.cpp is not the member function of the Polynomial. You must implement this function as member of Polynomial like this

void Polynomial::add(Term T){ ... } 

Comments

1

I think this is a syntax error. First, you defined the add method in the Polynomial class of the header file, but the CPP file did not add the class scope, which caused this problem. So you should adjust your code like this:

void Polynomial::add(Term t) { vect.push_back(t); } 

The root cause of this problem is that the methods of the class only work within the scope of the class, and if there is a function with the same name inside the class, it will lead to a naming conflict. Therefore, the root cause of this problem is not the reference error of the vector file.

1 Comment

To use C++ parlance, if you want to 'implement' the method add that you 'declared' to be part of your class Polynomial in a .cpp file, you need to use the correct syntax to tell the compiler that the method is part of the class. Otherwise it will not know to include other class members (e.g. vect) in the scope of the method implementation. Hence: void Polynomial::add(...). The method signature add(...) in the .cpp file tells the compiler that there is another method outside of the class Polynomial called add and that method will not have access to the members of Polynomial.
0

The issue is that instead of defining the members add and print of the class Polynomial, you are defining functions in global scope completely unrelated to the class Polynomial

Make changes in the function definition of void add(Term) and void print() to void Polynomial::add(Term) and void Polynomial::print().

#include "term.hpp" #include "Polynomial.hpp" #include<iostream> #include<map> using namespace std; void Polynomial::add(Term t) { // change here vect.push_back(t); } void Polynomial::print() { //change here for(int i = 0; i < vect.size(); i++) { cout << vect[i].toString(); } } Polynomial combineLikeTerms() { Polynomial poly; map<int, int> combinedPoly; for(int j = 0; j < vect.size(); j++) { combinedPoly.insert(pair<int, int>(vect[j].getExponent(), vect[j].getCoefficient()); } for(map<int,int>::iterator itr = combinedPoly.begin(); itr != combinedPoly.end(); itr++) { Term newTerm(itr->second, "x", itr->first); poly.add(newTerm); } return poly; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.