使用箭头(->)实现模板链表类

Implementing a template Linked List class using arrow (->)

本文关键字:实现 链表 gt      更新时间:2023-10-16

我在实现模板链表类时遇到问题。我以前做过链接列表,但不是作为模板。

在.cpp文件(实现)中,成员指针变量似乎没有被识别为成员,并且在这些指针之后使用->不起作用。问题附近没有语法错误的下划线,但当我突出显示变量时,描述显示"未知"和变量的详细信息

(更奇怪的是,我在范围中放置的任何东西都不会出现下划线语法错误。)

// ListLinked.h
    #include <iostream>
    using namespace std;
template <typename DataType>
class List {
private:
    struct ListNode {
        DataType dataItem;
        ListNode* next;
    };
    ListNode* head;
    ListNode* cursor;
public:
    List(int ignored = 0);  
    List(const List& other);
    List& operator=(const List& other);
    ~List();                                    
    void insert(const DataType& newDataItem);
    void remove();                                  
    void replace(const DataType& newDataItem);
    void clear();
    bool isEmpty() const;
    bool isFull() const;
    void gotoBeginning();                           
    void gotoEnd();                                 
    bool gotoNext();                                
    bool gotoPrior();                               
    void showStructure() const;
    DataType getCursor() const;                         
};
//ListLinked.cpp (just the simplest one of the many member functions that use ->)
template <typename DataType>
bool List<DataType>::gotoNext(){
if (cursor->next != NULL)      //"next" is not seen as a member pointer variable
    cursor = cursor->next;
}
// all the other member functions start with
// template <typename DataType>
// [type] List<DataType>:: {...}

我怀疑我的视觉表达2010出了问题,但我不确定

当您使用模板化的类/函数时,您需要在源文件中拥有整个实现。将所有模板引用放在.h文件中作为第一步;)

如果您想在没有函数代码的情况下保持头的整洁,可以创建一个名为linkedlist.impl的文件并放入您的定义中。在您的标题末尾,在您的后卫之前,添加#include "linkedlist.impl"