Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;将std::pair添加到模板基类 我在C++代码中有一种奇怪的行为。我有一个抽象的基类符号: 模板 类符号 { 受保护的: 向量单项式; 公众: 虚~符号(){} 虚拟ostream&print(ostream&o)常数=0; 虚空加法单项式(C,E)=0; };_C++_Vector_Polymorphism_Abstract_Std Pair - Fatal编程技术网

C++;将std::pair添加到模板基类 我在C++代码中有一种奇怪的行为。我有一个抽象的基类符号: 模板 类符号 { 受保护的: 向量单项式; 公众: 虚~符号(){} 虚拟ostream&print(ostream&o)常数=0; 虚空加法单项式(C,E)=0; };

C++;将std::pair添加到模板基类 我在C++代码中有一种奇怪的行为。我有一个抽象的基类符号: 模板 类符号 { 受保护的: 向量单项式; 公众: 虚~符号(){} 虚拟ostream&print(ostream&o)常数=0; 虚空加法单项式(C,E)=0; };,c++,vector,polymorphism,abstract,std-pair,C++,Vector,Polymorphism,Abstract,Std Pair,在我的派生类Theta2中,我想实现add_单项式方法,在该方法中,我尝试将一对推回向量单项式 class Theta2:公共符号 { 受保护的: 尺寸; 公众: θ2(尺寸n); θ2(常数θ2&); ~Theta2(); ostream&print(ostream&print)const; void add_单项式(复c,双e); }; ostream&operator感谢所有评论。这就是解决办法。 问题是我的副本构造函数中缺少mpc\u t c的初始化: Complex::Complex(

在我的派生类Theta2中,我想实现add_单项式方法,在该方法中,我尝试将一对推回向量单项式

class Theta2:公共符号
{
受保护的:
尺寸;
公众:
θ2(尺寸n);
θ2(常数θ2&);
~Theta2();
ostream&print(ostream&print)const;
void add_单项式(复c,双e);
};

ostream&operator感谢所有评论。这就是解决办法。 问题是我的副本构造函数中缺少mpc\u t c的初始化:

Complex::Complex(const Complex和其他)
{
mpc_init2(this->c,Complex::PREC);
mpc\u集合(此->c,其他.c,mpc\u RNDNN);
}

如果没有
Complex
定义,我们就不能说什么,复制构造函数可能是错误的。Thx@ForEveR我已经编辑了我的问题。
Complex
需要赋值运算符。另外,你的复制构造函数不会初始化
c
。我建议你在
valgrind
下运行你的代码
void Theta2::add_monomial(Complex c, double e)
{
    this->monomials.push_back(make_pair(c, e));
}
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;
}
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);
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;
}