By Retze

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)



using namespace std;

class Polynomial
    Polynomial(int deg, std::vector<int> coeff);
    Polynomial(const Polynomial & p);
    const int getCoeff(int index);

    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)
    std::vector<int> coefficient(;
    for ( int; i>= 0; i-- )
        coefficient[i] = p.coefficient[i];

const int Polynomial::getCoeff(int index)
    return coefficient[index];

    //delete[] & coefficient;

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++)

    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.


@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:

Polynomial::Polynomial(int deg, std::vector<int> const& coeff)
    : degree(deg)
    , coefficient(coeff)

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

Related Questions

Sponsored Content

30 Answered Questions

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

17 Answered Questions

[SOLVED] Concatenating two std::vectors

24 Answered Questions

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

4 Answered Questions

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

25 Answered Questions

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

17 Answered Questions

[SOLVED] How to find out if an item is present in a std::vector?

  • 2009-02-20 21:58:41
  • Joan Venge
  • 729820 View
  • 506 Score
  • 17 Answer
  • Tags:   c++ vector std

1 Answered Questions

[SOLVED] Intercepting an std::out_of_range error at throw time

1 Answered Questions

[SOLVED] How to return a static const int std::array from a method?

  • 2018-03-19 19:50:17
  • Roger_88
  • 99 View
  • -1 Score
  • 1 Answer
  • Tags:   c++ arrays c++11

2 Answered Questions

[SOLVED] storing objects of a different class in a vector in C++

  • 2011-03-09 03:51:33
  • user205834
  • 1484 View
  • 0 Score
  • 2 Answer
  • Tags:   object vector

11 Answered Questions

[SOLVED] Initializing a static std::map<int, int> in C++

  • 2008-09-26 10:10:45
  • Nithin
  • 318473 View
  • 347 Score
  • 11 Answer
  • Tags:   c++ stdmap

Sponsored Content