C++ 当我尝试向列表中添加新节点时,会收到错误消息“quot;没有匹配的函数调用“;
我正在尝试编写一个函数,将具有等效指数值的多项式项相加,并将该项放入一个新的链表中。我知道我这样做的逻辑还不正确,但我至少希望看到我可以将新术语添加到新列表中。但每次我尝试这样做时,都会收到错误消息C++ 当我尝试向列表中添加新节点时,会收到错误消息“quot;没有匹配的函数调用“;,c++,linked-list,nodes,C++,Linked List,Nodes,我正在尝试编写一个函数,将具有等效指数值的多项式项相加,并将该项放入一个新的链表中。我知道我这样做的逻辑还不正确,但我至少希望看到我可以将新术语添加到新列表中。但每次我尝试这样做时,都会收到错误消息no matching function call to polymonery::Term::Term 下面是我试图编写的函数。我在一行上得到了一个错误,我将头设置为等于一个新术语 Polynomial Polynomial::operator+( const Polynomial &othe
no matching function call to polymonery::Term::Term
下面是我试图编写的函数。我在一行上得到了一个错误,我将头设置为等于一个新术语
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
double sum;
shared_ptr<Polynomial> result = shared_ptr<Polynomial>(new Polynomial()); // where you are going to store your results
shared_ptr<Polynomial::Term> a = this->head;
shared_ptr<Polynomial::Term> b = other.head;
while(a != nullptr && b != nullptr)
{
for(a; a != nullptr; a=a->next)
{
for(b; b!=nullptr; b=b->next)
{
if(a->exponent == b->exponent)
{
sum = a->coeff+b->coeff;
head = shared_ptr<Term>(new Term( sum, exp, head ));
}
}
}
}
多项式::运算符+(常数多项式和其他)常数
{
双和;
shared_ptr result=shared_ptr(新多项式());//将结果存储在何处
共享\u ptr a=此->头部;
共享_ptr b=其他头;
while(a!=nullptr&&b!=nullptr)
{
对于(a;a!=nullptr;a=a->next)
{
对于(b;b!=nullptr;b=b->next)
{
如果(a->指数==b->指数)
{
总和=a->coeff+b->coeff;
head=共享的ptr(新术语(总和、经验、head));
}
}
}
}
头文件
#ifndef H_POLYNOMIAL_H
#define H_POLYNOMIAL_H
#include <ostream> // to be able to declare overloaded << operator as a friend
#include <string> // input type to the main constructor
#include <tr1/memory> // for shared_ptr
#include <tr1/shared_ptr.h>
using namespace std;
class Polynomial
{
friend ostream &operator<<( ostream &, const Polynomial & );
public:
Polynomial(); // default polynomial is empty (= 0)
Polynomial( string & ); // Set the polynomial according to the string
Polynomial &operator=( const Polynomial & ); // assignment
bool operator==( const Polynomial & ) const; // equality test
bool operator!=( const Polynomial & ) const; // not equal test
Polynomial operator+( const Polynomial & ) const; // addition
double eval( double x ) const ; // evaluate the polynomial at x
private:
class Term // a Term of the polynomial
{
public:
Term( double c, int e, shared_ptr<Term> n );
double coeff; // the coefficient
int exponent; // the exponent
shared_ptr<Term> next;
};
shared_ptr<Term> head; // The head of the list
static double TOL; // Tolerance for floating point equality
};
#endif
\ifndefh\u多项式
#定义H_多项式
#包含//以便能够声明重载>系数>>exp)
如果(系数!=0)//不要做0项
head=共享的ptr(新术语(系数、经验、head));
}
据我所知,当您尝试在中使用变量时,还没有声明变量exp
new Term( sum, exp, head )
我猜您的意思是
b->exponent
或什么的。除了您没有声明exp
变量,而且您似乎对值语义有一些混淆之外,这是无效的:
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
//...
head = shared_ptr<Term>(new Term( sum, exp, head ));
}
多项式::运算符+(常数多项式和其他)常数
{
//...
head=共享的ptr(新术语(总和、经验、head));
}
此赋值无效,因为函数标记为
const
,因此head
是const std::shared\u ptr
。你可能不想要一个变异的操作符+
,所以我建议复制多项式
,并对其进行操作。最好通过定义一个操作符+=
并在数据副本上实现它。但是即使我尝试复制多项式,这不需要创建一个新的多项式对象吗?是的。我认为创建副本远比修改a+b
更可取a
operator+
也应该是一个非成员函数,这样它可以与左侧的隐式转换一起工作。请看一下。感谢您提供的链接。我只是完全搞不清楚如何复制多项式对象。特别是如果我现在调用术语构造函数来创建新术语时遇到问题。
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
//...
head = shared_ptr<Term>(new Term( sum, exp, head ));
}