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.
 
    