问题描述
我正在尝试编写一个处理多项式乘法,除法等的类。
void poly_Mult(const polynomial& poly);
void poly_Divide(const polynomial& poly);
还有两个朋友功能:
friend polynomial poly_Mult(const polynomial& poly1,const polynomial& poly2);
friend polynomial poly_Divide(const polynomial& poly1,const polynomial& poly2);
我已经毫无问题地定义了两个poly_Mult函数,并且尝试使用它们来定义poly_Divide函数。 对于朋友功能,我没有问题。但是,在成员函数中:
bool polynomial::poly_Divide(const polynomial& poly)
{
if (poly.m_Degree == 0)
return false;
polynomial result;
result.m_Degree = this->m_Degree - poly.m_Degree;
result.m_Variable = this->m_Variable;
polynomial reduced(*this);
double GuidingFactor = poly.m_Terms[poly.size() - 1].Factor();
while (reduced.m_Degree >= poly.m_Degree)
{
int exp = reduced.m_Degree - poly.m_Degree;
double termFactor = reduced.m_Terms[reduced.size() - 1].Factor() / GuidingFactor;
//I get an error on this next line. The compiler doesn't seem to find to right overloaded function for poly_Mult
reduced.poly_Subtract(poly_Mult(poly,polynomial(termFactor,poly.m_Variable,exp)));
result.m_Terms.push_back(VarPower(termFactor,exp));
reduced.Simplify();
}
result.Simplify();
*this = result;
if(reduced.size() == 1 && reduced.m_Terms[0].Factor() == 0 && reduced.m_Terms[0].Exponent() == 0)
return true;
else
return false;
}
该代码在Friendly函数中的功能相同,并且可以按预期完美运行,没有错误。
而不是整个详细功能,因为Friendly函数可以正常工作,但是同样,编译器似乎找不到正确的重载函数。 我确实没有在这里看到问题,因为函数具有不同的参数,所以我看不出模棱两可。
通过简化代码,我遇到了同样的问题:
class polynomial
{
private:
int m_Degree;
public:
polynomial(int degree)
: m_Degree(degree) {};
polynomial(const polynomial& poly)
: m_Degree(poly.m_Degree) {};
polynomial& operator=(const polynomial& poly)
{
m_Degree = poly.m_Degree;
}
void Multiply(const int& number);
friend polynomial Multiply(const polynomial& Object,const int& number);
void Divide(const int& number);
friend polynomial Divide(const polynomial& Object,const int& number);
};
#include "polynomial.h"
void polynomial::Multiply(const int& number)
{
m_Degree *= number;
}
polynomial Multiply(const polynomial& Object,const int& number)
{
return polynomial(Object.m_Degree * number);
}
void polynomial::Divide(const int& number)
{
polynomial copy = *this;
polynomial result = Multiply(copy,(1./number)); //This is where the problem is. The compiler doesn't find the right overloaded function
*this = result;
}
polynomial Divide(const polynomial& Object,const int& number)
{
polynomial result = Multiply(Object,(1. / number));
return result;
}
我得到那些错误:
不存在合适的构造函数来将“ void”转换为“多项式”
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)