2018-02-13 13:05:11 8 Comments

I'm fairly new to C++ and wrote a polynomial class using std::vector. Everything works fine, until I try to invoke the function getCoeff(int index) which should return the coefficient at a specific index. In my case getCoeff(0) should return the 0th coefficient which is '1'.

Instead I receive this error when compiling with g++:

```
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >=this-> size() (which is 0)
Aborted
```

Polynomial.h:

```
#include<iostream>
#include<vector>
using namespace std;
class Polynomial
{
public:
Polynomial(int deg, std::vector<int> coeff);
Polynomial(const Polynomial & p);
~Polynomial();
const int getCoeff(int index);
private:
int degree;
std::vector<int> coefficient;
};
Polynomial::Polynomial(int deg, std::vector<int> coeff)
{
degree = deg;
std::vector<int> coefficient(deg);
for( int i = degree; i >= 0; i--)
coefficient[i] = coeff[i];
}
Polynomial::Polynomial(const Polynomial & p)
{
degree=p.degree;
std::vector<int> coefficient(p.degree);
for ( int i=p.degree; i>= 0; i-- )
coefficient[i] = p.coefficient[i];
}
const int Polynomial::getCoeff(int index)
{
return coefficient[index];
}
Polynomial::~Polynomial()
{
//delete[] & coefficient;
coefficient.clear();
}
```

And the main file, in which I created a test Polynomial test1 which has a degree of 3 and the coefficients 1,9,3,4 (Note: the std::vector coeff1 is constructed out of the array ko1[ ]):

```
int main ()
{
int ko1[] = {1,9,3,4};
int degree1 = sizeof(ko1)/sizeof(*ko1)-1;
std::vector<int> coeff1;
for (int i=0; i<= degree1; i++)
coeff1.push_back(ko1[i]);
Polynomial test1(degree1, coeff1);
cout << "Coefficients: " << endl;
for (int j=0; j<=degree1; j++)
cout << coeff1[j] << endl;
cout << test1.getCoeff(0); //this is where the error occurs
return 0;
}
```

I suspect there is an error in my constructor, or the elements obtained from the array are not accepted in the new std::vector Thank you for your help.

### Related Questions

#### Sponsored Content

#### 29 Answered Questions

### [SOLVED] How to concatenate a std::string and an int?

**2008-10-10 15:05:05****Obediah Stane****559206**View**594**Score**29**Answer- Tags: c++ int concatenation stdstring

#### 18 Answered Questions

### [SOLVED] Concatenating two std::vectors

**2008-10-14 15:46:01****yigal****275485**View**544**Score**18**Answer- Tags: c++ vector stl concatenation stdvector

#### 26 Answered Questions

### [SOLVED] Easiest way to convert int to string in C++

**2011-04-08 04:19:41****Nemo****2391581**View**1329**Score**26**Answer- Tags: c++ string int type-conversion

#### 11 Answered Questions

#### 4 Answered Questions

### [SOLVED] How can I get sizeof a vector::value_type?

**2014-01-22 18:21:53****Chris****6765**View**19**Score**4**Answer- Tags: c++ c++11 vector sizeof value-type

#### 25 Answered Questions

### [SOLVED] What is the easiest way to initialize a std::vector with hardcoded elements?

**2010-02-10 10:55:33****Agnel Kurian****719636**View**546**Score**25**Answer- Tags: c++ vector initialization

## 1 comments

## @Maxim Egorushkin 2018-02-13 13:10:31

One recurring error in you code is that it assumes that a vector has elements with indices

`[0, size]`

, whereas it is a half-open range`[0, size)`

. E.g. those loops in constructors should really be`for(int i = degree; i-- > 0;)`

or`for(int i = 0; i < degree; ++i)`

to avoid accessing a non-existent element at index`degree`

.Another is that constructor

`Polynomial(int deg, std::vector<int> coeff)`

fails to initialize`coefficient`

member variable, it initializes a local variable instead. Fix:Because

`degree`

is the number of coefficients, you can remove member`degree`

and use`coefficient.size()`

instead of it.The code does not need a custom copy constructor or destructor, you can safely remove them or, if necessary, declare them as

`= default`

.## @Borgleader 2018-02-13 13:13:52

I'm not so sure about that first part. OP push_backs elements using the same condition as they do for iteration.

## @Maxim Egorushkin 2018-02-13 13:14:53

@Borgleader See loops in constructors.

## @user1810087 2018-02-13 13:17:23

@MaximEgorushkin that's also half true. see the call of the ctor

`Polynomial test1(degree1, coeff1);`

while degree1 is`int degree1 = sizeof(ko1)/sizeof(*ko1)-1;`

However, you are right it is missleading using`[0, size]`

inside the class...## @Retze 2018-02-13 13:45:18

@MaximEgorushkin Thanks a lot, it works now as it should. However, I do not understand exactly why, meaning I don't know what the part ": degree(deg) , coefficient(coeff)" means.

## @Maxim Egorushkin 2018-02-13 13:50:19

@Retze That is initializer list syntax, see en.cppreference.com/w/cpp/language/initializer_list.