如何修复导致此错误的函数定义与我的嵌入式 class/struct:无法推断 'T' 的模板参数?

How do I fix my function definition that causes this error with my embedded class/struct: could not deduce template argument for 'T'?

我实现了一个名为 Node 的链表,它有一个名为 freeData 的函数,它将在给定节点和任何后续节点上执行删除。

我想作为私有成员在我自己的自定义 list class 中实现它,但在 Visual Studio 2019 年遇到了这个错误:

C2672 'freeData': no matching overloaded function found

C2783 'void custom::freeData(list::Node*&): could not deduce template argument for 'T'

我不知道要为我的 freeData 函数头更改什么以接受 Node* 作为参数。我在这些函数中传递参数 pHead~list()clear().

freeData 嵌入 list class 之前的先前定义是 void freeData(Node <T>* &pHead)

#include <iostream>

namespace custom
{

   template <class T>
   class list
   {
   public:
      list() : numElements(0), pHead(NULL), pTail(NULL) { }
      ~list() { freeData(pHead); }

      void clear() { freeData(pHead); numElements = 0; pHead = NULL; pTail = NULL; }

   private:
      struct Node;
      Node* pHead;
      Node* pTail;
      int numElements;
   };

   template <class T>
   struct list <T> :: Node
   {
      Node() : pNext(NULL), pPrev(NULL) {}
      Node(const T& t) : data(t), pNext(NULL), pPrev(NULL) {}

      T data;      // data of type T
      Node* pNext; // pointer to next node
      Node* pPrev; // pointer to previous node
   };

   template <class T>
   void freeData(typename list <T>::Node*& pHead)
   {
   }

} // end of namespace

int main()
{
   custom::list <int> l1;
   l1.clear();

   return 0;
}

freedata() 是一个独立的函数。与 class 方法不同,独立函数必须在使用前声明。但是,在这种情况下,您不能前向声明 freedata(),因为它的参数取决于需要知道 freedata() 是什么的类型。第二十二条军规。

要解决这个问题,您可以分解 listNode class 的声明和实现,例如:

#include <iostream>

namespace custom
{

   template <class T>
   class list
   {
   public:
      list();
      ~list();

      void clear();

   private:
      struct Node
      {
         Node();
         Node(const T& t);

         T data;      // data of type T
         Node* pNext; // pointer to next node
         Node* pPrev; // pointer to previous node
      };

      Node* pHead;
      Node* pTail;
      int numElements;
   };

   template <class T>
   void freeData(typename list <T>::Node*& pHead)
   {
      ...
   }

   template <class T>
   list<T>::list() : numElements(0), pHead(NULL), pTail(NULL) { }

   template <class T>
   list<T>::~list() { freeData(pHead); }

   template <class T>
   void list<T>::clear() { freeData(pHead); numElements = 0; pHead = NULL; pTail = NULL; }

   template <class T>
   list<T>::Node::Node() : pNext(NULL), pPrev(NULL) {}

   template <class T>
   list<T>::Node::Node(const T& t) : data(t), pNext(NULL), pPrev(NULL) {}

} // end of namespace

int main()
{
   custom::list <int> l1;
   l1.clear();

   return 0;
}

但实际上,freedata() 没有理由成为本例中的独立函数。它应该是 list class 的成员,例如:

#include <iostream>

namespace custom
{

   template <class T>
   class list
   {
   public:
      list() : numElements(0), pHead(NULL), pTail(NULL) { }
      ~list() { clear(); }

      void clear() { freeData(pHead); numElements = 0; pHead = NULL; pTail = NULL; }

   private:
      struct Node
      {
         Node() : pNext(NULL), pPrev(NULL) {}
         Node(const T& t) : data(t), pNext(NULL), pPrev(NULL) {}

         T data;      // data of type T
         Node* pNext; // pointer to next node
         Node* pPrev; // pointer to previous node
      };

      Node* pHead;
      Node* pTail;
      int numElements;

      static void freeData(Node*& pHead)
      {
         ...
      }
   };

} // end of namespace

int main()
{
   custom::list <int> l1;
   l1.clear();

   return 0;
}