当我尝试将新节点添加到列表中时,我收到错误消息"no matching function call"

When I try to add a new node to the list, I get error message "no matching function call"

本文关键字:错误 消息 function matching no call 列表 新节点 节点 添加      更新时间:2023-10-16

我正在尝试编写一个函数,该函数将具有等效指数值添加到多项式项中,并将该项放在新的链表中。我知道我这样做的逻辑还不正确,但我至少希望看到我可以将新术语添加到新列表中。每次我尝试这样做时,我都会收到错误消息no matching function call to Polynomial::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 ));
                    }
                }
            }
       } 

头文件

#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

类实现

Polynomial::Term::Term( double c, int e, shared_ptr<Term> n )
   {
   coeff = c; exponent = e; next = n;
   }
//+--------------------------------------+
//| Default Constructor: Polynomial is 0 |
//+--------------------------------------+
Polynomial::Polynomial()
   {
   head = nullptr;
   }
//+-------------------------------------------------------------+
//| Constructor: The input string contains coefficient-exponent |
//| pairs where everything is separated by whitespace           |
//+-------------------------------------------------------------+
 Polynomial::Polynomial( string & str )
   {
   stringstream ss( str );  // stringstream lets us extract items separated by
                            // whitespace simply by using the >> operator
   double coefficient;      // to hold the coefficient
   int exp;                 // to hold the exponent
   head = nullptr;          // initialize head to null
   // read in coefficient-exponent pairs and add each term to the list
   // ----------------------------------------------------------------
   while (ss >> coefficient >> exp)
      if (coefficient != 0)   // don't make a 0 term
         head = shared_ptr<Term>(new Term( coefficient, exp, 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 ));
}

此赋值无效,因为函数标记为 const ,因此head是一个const std::shared_ptr。你可能不想要一个变异的operator+,所以我建议复制Polynomial并对其进行操作。最好通过定义operator+=并根据数据副本实现