How to traverse vector in class

I am trying to find the largest exponent in this vector, but I am getting an error message. The whole program is very long, so I will just post the relevant pieces.

#include <iostream>
#include <string>
#include <cmath>
#include <vector>

using namespace std;

class Term
{
private:
int coeff;
int exp;

public:
void setCoeff(const int coefficient);
void setExp(const int exponent);
int getCoeff() const;
int getExp() const;
Term derivative() const;
double eval(const double x) const;
void displayFirst() const;
void display() const;
};

class Poly
{
private:
vector<Term> terms;

public:
void addTerm(const int coeff, const int exp);
void scale(const int fact);

Term getTerm(const int index) const;
int degree() const;
int termCount() const;
double eval(const double x) const;
Poly derivative() const;
Poly add(const Poly & poly) const;
Poly subtract(const Poly & poly) const;
void display(const string & label) const;
};
//This function should return the highest degree polynomial
int Poly::degree() const
{
Poly terms;

for(int i = 0; i < terms.size(); i++) //Error; No member named size in poly
{

}


}



You're creating a new Poly object called terms in this function. The poly object has no member function size.

1
2
3
4
5
6
7
8
int Poly::degree() const
{
Poly terms;

for(int i = 0; i < terms.size(); i++) //Error; No member named size in poly
{

}



Do you mean to access the terms vector of a Poly object and find the size of the vector?
1
2
3
4
class Poly
{
private:
vector<Term> terms;
Yes I think so. How would I go about doing that?
Can you just post the whole code please? That would be much easier
Remove line 3 Poly terms; or change line 5 to this->terms.size(); You could do both if you wish but I would just suggest removing the unnecessary line 3.
Topic archived. No new replies allowed.