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

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:

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 en.cppreference.com/w/cpp/language/initializer_list.

Related Questions

Sponsored Content

27 Answered Questions

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

22 Answered Questions

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

2 Answered Questions

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

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

24 Answered Questions

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

16 Answered Questions

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

  • 2009-02-20 21:58:41
  • Joan Venge
  • 659879 View
  • 455 Score
  • 16 Answer
  • Tags:   c++ vector std

5 Answered Questions

[SOLVED] Why is Java Vector class considered obsolete or deprecated?

14 Answered Questions

[SOLVED] Concatenating two std::vectors

11 Answered Questions

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

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

2 Answered Questions

[SOLVED] C++ Vectors: Why is this piece of code not working?

  • 2011-03-28 17:56:29
  • cBMtb
  • 252 View
  • 0 Score
  • 2 Answer
  • Tags:   c++ arrays vector

3 Answered Questions

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

Sponsored Content