[英]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
的 function ,它將在給定的節點和任何后續節點上執行刪除。
我想在我自己的自定義list
class 作為私有成員中實現它,但在 Visual Studio 2019 中出現了這個錯誤:
C2672 'freeData':找不到匹配的重載 function
C2783 'void custom::freeData(list::Node*&): 無法推斷 'T' 的模板參數
我不知道要為我的freeData
function header 更改什么以接受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()
是一個獨立的 function。 與 class 方法不同,必須先聲明獨立函數,然后才能使用它們。 但是,在這種情況下,您不能前向聲明freedata()
,因為它的參數取決於需要知道什么是freedata()
的類型。 第 22 條軍規。
要解決這個問題,您可以分解list
和Node
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()
沒有理由成為獨立的 function。 它應該是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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.