C++ Qt重载运算符

C++ Qt重载运算符,c++,operator-overloading,C++,Operator Overloading,我正在尝试编写一个多项式类,可以用于计算 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 operato

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

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;
};
与+和相同-

这可能吗?它可以让我用多项式来计算,就像用双精度多项式一样


请提前感谢

您的运算符只能按您定义的方向运行。正如您的类所示,左侧始终是一个多项式,它不适用于
double*多项式
,其中double位于左侧

由于多项式的乘法是可交换的(我认为这是单词…
a*b=b*a
),因此可以在类外定义这样的运算符:

Polynomial operator+(const double& lhs, const Polynomial& rhs) {
    return rhs + lhs; //switch the operation so that the polynomial is on the left hand side
}
它将把双精度线作为左手边,并像右手边一样应用它

根据这个答案改编:


链接的答案还解释了如果操作不是可交换的,那么如何进行操作。

你能把你的问题整理一下吗。我假设它应该是多项式p(“3*x^2+x^1-1”);而不是多项式p(3*x^2+x^1-1);你想让p=p*2/这个有效还是p=p*a/这个有效???很难说清楚你的问题是什么我认为这应该能回答你的问题[Binary Multiplication Operator(StackOverflow)][1][1]:
Polynomial operator+(const double& lhs, const Polynomial& rhs) {
    return rhs + lhs; //switch the operation so that the polynomial is on the left hand side
}