2

在 C++ 中创建一些旧的数据结构。目前我遇到了一个双向链表类的问题:

列表.h:

template <class T>
class List{

private:

int size;

struct listNode{                        
    T data;
    listNode* next;
    listNode* prev;
    listNode(T newData);
};

listNode * head;                    
listNode * tail;                    
listNode * curr;                    
listNode * find(listNode * place, int k);   
void removeCurrent(listNode * temp);    
public:

List(); 
int getSize() const;                
void insert(int loc, T data);       
void remove(int loc);           
T const & getItem(int loc) const;   
void print();

};

列表.cpp:

#include "List.h"
#include <iostream>

using namespace std;

template<class T>
List<T>::List(){
size = 0;
head->next = tail;
head->prev = NULL;
tail->prev = head;
tail->next = NULL;


 }

// getSize: public method that returns the size of the list
template<class T>
int List<T>::getSize() const {
    return size;
}

// insert: public method that inserts data into the list
template<class T>
void List<T>::insert(int loc, T data){
    if(loc <1){
        cout<<"Invalid Location"<<endl;
        return;
    }
    curr = find(head,loc-1);
    listNode * newNode = new listNode(data);
    newNode->next = curr->next;
    newNode->prev = curr;
    newNode->next->prev = newNode;
    curr->next = newNode;
    size++;
}

// remove: public method that inserts data into the list
template<class T>
void List<T>::remove(int loc){
    if(loc <1){
        cout<<"Invalid Location"<<endl;
        return;
    }
    curr = find(head,loc);  // Find the node infront of the target
    removeCurrent(curr);    // Remove that node
}

// removeCurrent: helper function that removes the current node
template<class T>
void List<T>::removeCurrent(listNode* temp){
    listNode* t = temp->next;
    temp->data = t->data;       // HACK: take data from next node
    temp->next = t->next;
    t->next->prev = temp;
    delete t;
    t=NULL;
    size--;
}

// find: private helper function that returns a pointer to the k-1 node
template<class T>
listNode * List<T>::find(listNode * place, int k){
    if((k==0) || (place==NULL))
        return place;
    else return find(place->next,k-1);
}

// getItem: returns data at location loc
template<class T>
T const& List<T>::getItem(int loc) const{
    curr = find(head,loc);
    return curr->data;
}

// print: prints the sequence of variables in the list
template<class T>
void List<T>::print()
{
    curr = head;
    while(curr->next != tail){
        curr = curr->next;
        cout<<curr->data<<endl;
    }
}

//listNode constructor
template<class T>
List<T>::listNode::listNode(T newdata):data(newdata),next(NULL),prev(NULL)
{}

我得到的错误如下:

错误:“listNode”没有命名类型。

我尝试了类似故障排除帖子中提供的不同建议,但我仍然收到此错误。我有一个包含 List.cpp 的 main.cpp,但它实际上是空的。

4

1 回答 1

3

您将不得不listNode在方法的返回类型中指定您正在谈论的内容,find因为您将其定义为List类的成员并且您还必须使用typename(因为List<T>是依赖范围)。

template <class T>
typename List<T>::listNode* List<T>::find(listNode* place, int k)
{
    if ((k == 0) || (place == NULL))
        return place;
    else
        return find(place->next, k-1);
}

假设您使用的是 c++11,您可能还想使用,nullptr而不是因为它更安全,并在构造函数NULL中使用初始化列表。List

于 2013-02-06T11:37:15.717 回答