gpt4 book ai didi

c++ - Qt 重载运算符

转载 作者:行者123 更新时间:2023-11-28 07:13:52 25 4
gpt4 key购买 nike

<分区>

我正在尝试编写一个可用于计算的多项式类。

class Polynomial
{
public:
Polynomial(QString s); // creates a polynomial with QRegExp
const Polynomial operator+(Polynomial const& rhs); // NOT TESTED
const Polynomial operator+(double d);
const Polynomial operator-(Polynomial const& rhs); //
const Polynomial operator-(double d);
const Polynomial operator-();
private:
void resizeToMin();
QList<int> exp;
QList<double> coeff;
QChar var;
};

我想像这样使用多项式:

 Polynomial p("3*x^2 + x^1 -1");
double a = 2.0;
p = p*2 // this works
p = p*a // this works
p = 2*p // DOES NOT WORK
p = a*p // DOES NOT WORK

+ 和 - 相同

这可能吗?它将允许我以与 double 相同的方式使用多项式进行计算

提前致谢

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com