当我尝试将新节点添加到列表时,我收到错误消息“没有匹配的函数调用”

时间:2015-03-11 14:51:43

标签: c++ linked-list nodes

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

2 个答案:

答案 0 :(得分:0)

据我所知,当您尝试在

中使用变量时,您尚未声明变量exp
new Term( sum, exp, head )

我猜你的意思是b->exponent或其他什么。

答案 1 :(得分:0)

除了您没有声明exp变量并且您似乎对值语义有些混淆之外,这是无效的:

Polynomial Polynomial::operator+( const Polynomial &other ) const
{
    //...
    head = shared_ptr<Term>(new Term( sum, exp, head ));
}

此分配无效,因为该功能已标记为const,因此headconst std::shared_ptr。您可能不想要变异operator+,因此我建议您复制Polynomial并对其进行操作。优选地,通过定义operator+=并在数据副本上实现该术语。