1
我希望這個問題在之前的某個問題中沒有涉及。我盡我所能,但我認爲首先問題的一部分是我不明白到底發生了什麼,這可能阻止我找到以前的答案。我很抱歉,但如果是的話,但除此之外...爲什麼使用模板類型的此模板類擁有的類的類型無法識別?
對於練習模板和更好地理解C++和代碼設計,我已經開始編寫一個鏈接列表的實現(目前很簡單),主要是模仿std ::名單。我一直在努力正確地實現迭代器,並在邏輯上實現其他組件,但是我碰到了一個障礙。我猜這是在某處使用模板語法,但我不確定。這可能只是一個愚蠢的錯誤。
下面是類的一般結構:
template <class T>
class LinkedList {
public:
LinkedList();
class Iterator;
void push_front(const T&);
void push_back(const T&);
void pop_front();
void pop_back();
T& front();
T& back();
unsigned int size() const;
bool empty() const;
Iterator begin();
Iterator end();
private:
struct ListNode;
ListNode* m_front;
ListNode* m_back;
unsigned int m_size;
};
template <class T>
class LinkedList<T>::Iterator {
public:
Iterator();
Iterator(const Iterator& rhs);
Iterator(ListNode* const& node);
Iterator operator=(const Iterator& rhs);
T& operator*();
bool operator==(const Iterator& rhs) const;
bool operator!=(const Iterator& rhs) const;
Iterator operator++();
private:
ListNode* m_node;
};
template <class T>
struct LinkedList<T>::ListNode {
T* m_data;
ListNode* m_next;
};
,這裏是有問題的功能:
template <class T>
void LinkedList<T>::push_front(const T&) {
if (m_front == NULL) {
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = NULL;
m_back = m_front;
} else if (m_front == m_back) {
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = m_back;
} else {
ListNode* former_front(m_front);
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = former_front;
}
}
和錯誤由GCC 4.6.3給出:
linkedlist.hpp: In member function ‘void pract::LinkedList<T>::push_front(const T&)’:
linkedlist.hpp:75:31: error: expected primary-expression before ‘;’ token
linkedlist.hpp:80:31: error: expected primary-expression before ‘;’ token
linkedlist.hpp:85:31: error: expected primary-expression before ‘;’ token
我希望所有的幫助,但如果還有其他什麼是可取的,請不要問。 謝謝大家。
Gah!我只知道這會是一些小錯誤。隨着模板和動態數據結構以及類的子類玩弄......我忘記命名一個參數。 但除此之外,非常感謝! – Orion
@Orion:沒問題。 : - ] – ildjarn