C++没有命名类型

C++ does not name a type

本文关键字:类型 C++      更新时间:2023-10-16

在C++中创建一些旧的数据结构。目前我有一个双重链接列表类的问题:

List.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();

};

List.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,但它实际上是空的。

您必须在find方法的返回类型中指定要谈论的listNode,因为您将其定义为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构造函数中使用初始值设定项列表。