Search code examples
c++vectorpolymorphismabstractstd-pair

C++ adding std::pair to template base class


I have a weird behavior in C++ code. I have an abstract base class Symbolic:

template <class C, class E>
class Symbolic
{
protected:
    vector<pair<C, E> > monomials;
public:
    virtual ~Symbolic() {}

    virtual ostream &print(ostream &o) const = 0;
    virtual void add_monomial(C, E) = 0;
};

In my derived class Theta2 I want to implement the method add_monomial in which I try to push back a pair to the vector monomials.

class Theta2 : public Symbolic<Complex, double>
{
protected:
    size_t n;

public:
    Theta2(size_t n);
    Theta2(const Theta2 &);

    ~Theta2();
    ostream& print(ostream &) const;

    void add_monomial(Complex c, double e);

};

ostream & operator<<(ostream &, const Theta2 &);

And that is the actual implementation of the method:

void Theta2::add_monomial(Complex c, double e)
{
    this->monomials.push_back(make_pair(c, e));
}

Complex is a type I've implemented as wrapper for the GNU MPC library.

And this is how I call the method:

int main (int argc, char **argv)
{
    Theta2 t2(7);

    cout << "Adding monomials" << endl;

    t2.add_monomial(Complex(1), 0);
    t2.add_monomial(Complex(2), 1);
    t2.add_monomial(Complex(3), 2);
    t2.add_monomial(Complex(4), 3);
    t2.add_monomial(Complex(5), 4);
    t2.add_monomial(Complex(6), 5);

    cout << "Theta2:" << t2 << endl;

    return EXIT_SUCCESS;
}

My actual problem is, that my program prints out "Adding monomials" but then terminates without any error message. I've debugged the program and it calls the method add_monomials but terminates after the push_back.

EDIT: This is my Complex definition:

enum ComplexPart {REAL, IMAG};

class Complex {

private:
    mpc_t c;

public:
    static const mpfr_prec_t PREC;

    Complex();
    Complex(long double re);
    Complex(long double re, long double im);
    Complex(const Complex &);

    virtual ~Complex();

    void add(const Complex &other);
    Complex add(const Complex &other) const;

    string get_mpfr_string(mpfr_t number, size_t digits) const;
    string get_cplx_part(ComplexPart which, size_t digits) const;

    Complex &operator=(const Complex &);

    virtual ostream& print(ostream &o) const;

};

ostream &operator<<(ostream &o, const Complex &c);

And this is the implementation:

const mpfr_prec_t Complex::PREC = 1024;

Complex::Complex()
{
    mpc_init2(this->c, Complex::PREC);
}

Complex::~Complex()
{
    mpc_clear(this->c);
}

Complex::Complex(const Complex &other)
{
    mpc_set(this->c, other.c, MPC_RNDNN);
}

Complex::Complex(long double re)
{
    mpc_init2(this->c, Complex::PREC);
    mpc_set_ld(this->c, re, MPC_RNDNN);
}

Complex::Complex(long double re, long double im)
{
    mpc_init2(this->c, Complex::PREC);
    mpc_set_ld_ld(this->c, re, im, MPC_RNDNN);
}

Complex &Complex::operator=(const Complex &c)
{
    mpc_set(this->c, c.c, MPC_RNDNN);
    return *this;
}

Solution

  • Thanks to all commenters. This is the Solution. The problem was the missing initialization of mpc_t c in my copy constructor:

    Complex::Complex(const Complex &other)
    {
        mpc_init2(this->c, Complex::PREC);
        mpc_set(this->c, other.c, MPC_RNDNN);
    }