0

我有一个关于我的两个功能的小问题。我的编译器一直说传递的函数是未声明的。这是编译器所说的:

LinkedList.hpp: In member function 'void LinkedList<T>::insert(int, T) [with T = std::basic_string<char>]':
test.cpp:92:1:   instantiated from here
LinkedList.hpp:96:2: error: 'struct ListNode<std::basic_string<char> >' has no member named 'item'
In file included from test.cpp:4:0:
LinkedList.hpp: In member function 'T LinkedList<T>::remove(int) [with T = std::basic_string<char>]':
test.cpp:92:1:   instantiated from here
LinkedList.hpp:139:2: error: 'struct ListNode<std::basic_string<char> >' has no member named 'theData'

在我的班级中有结构之前,但这不起作用。为什么它给我这些错误。

现在这里是代码:

#ifndef _CS235_LINKEDLIST_H_
#define _CS235_LINKEDLIST_H_
#define LIST_MAX 10


#include "ABCList.hpp"
#include<iostream>
using namespace std;

template <class T>
struct ListNode
    {
        T data; // List item
        ListNode *next; //Pointer to next node
    };

template <class T>
class LinkedList : public ABCList<T> {
private:

    int  size;
    ListNode<T> *head;
    ListNode<T> *find(int pos);
public:
    LinkedList();
    LinkedList(LinkedList &other);
    virtual ~LinkedList();


    virtual bool isEmpty ();
    virtual int  getLength ();
    virtual void insert (int pos, T item);
    virtual T    remove (int pos);
    virtual T    retrieve (int pos);
};

template <class T>
LinkedList<T>::LinkedList() //Default constructor
{
    size = 0;
    head = NULL;
}

template <class T>
LinkedList<T>::LinkedList(LinkedList &other) //Copy Constructor
{
    for(int i = 1; i < other.getLength(); i++)
        insert(i, other.retrieve(i));
}

template <class T>//Deconstructor
LinkedList<T>::~LinkedList()
{
    while(!isEmpty ())
        remove(1);
}

template <class T>
ListNode<T>* LinkedList<T>::find(int pos) //Finds the position of an item
{
    if(pos < 1)
        return NULL; //If pos is less than one then find returns NULL because pos is a illegal value.
    else
    {
        ListNode<T>* temp = head;
        for(int i = 1; i < pos; i++)
            {temp = temp -> next;}

        return temp;
    } 
}

template <class T>
bool LinkedList<T>::isEmpty ()//Function checks wheter if the list is empty
{
    if (size == 0)
        return true;
    return false;
}

template <class T>
int LinkedList<T>::getLength () //Function that returns size.
{
    return size;
}

template <class T>
void LinkedList<T>::insert (int pos, T item)// This function inserts a item.
{
    ListNode<T>* curr = new ListNode<T>(); //ListNode is inserted into the newly created node 
    curr -> item = item;
    if(pos ==1) 
    { //Node is added to the beginning
        curr -> next = head;
        head = curr;
    }
    else
    {
        ListNode<T>* prev = find(pos - 1);// A new node is placed after prev
        curr -> next = prev;
        prev -> next = curr;
        ++size;
    }
}

template <class T>  
T LinkedList<T>::remove (int pos)// This function deletes an item
{
    try
    {
        if(pos < 1 || pos > getLength()+1)// if the position of the list is one or greater than the list, it will throw an error.
            throw 99;
    }
    catch (int x)
        {cout << "Error Number: " << x;}

    T theData;
    ListNode<T>* curr = head;

    if(pos == 1) //The first node from the list is deleted.
    {
        curr = head; // This saves a pointer to node because when an item in the first position is deleted, a pointer is needed to precede it.
        head = head -> next;
    }

    else 
    {
        ListNode<T>* prev = find(pos - 1);//This node will be deleted after the node that prev points to
        curr = prev -> next; // The node is saved to a pointer
        prev -> next = curr -> next;
    }

    --size;
    theData = curr -> theData;
    delete curr;
    return theData;
}

template <class T>
T LinkedList<T>:: retrieve (int pos)
{
    try
    {
        if(pos < 1)// If the position is less than one an error will occur.
            throw 99;
    }
    catch (int x)
        {cout << "Error. Error Number: " << x;}

    if (pos >= 1)
    {
        ListNode<T>* curr = find(pos);  
        return curr-> data; //returns data to a node.
    }
}
#endif
4

3 回答 3

2

错误消息说明了一切。

导致错误的这一行访问struct的item成员ListNode

curr -> item = item;

ListNode没有这样的成员,可能您的意思是data代替item.

这同样适用于有错误的另一行

theData = curr -> theData;
于 2012-10-12T15:04:25.913 回答
0

嗯..编译器告诉你,根据你在错误中说的话

您对 line 的使用curr -> item在函数中无效,insert因为该结构没有一个名为item它的成员,它有一个名为data

curr -> theData出于同样的原因,您在函数 remove 中对 line 的使用无效...

还要检查你的插入,

我相信代码

curr ->  next = prev;
prev -> next = curr;

curr指出prev, 和prevto curr?! 闭环?!那是你要的吗?!

你不是说

curr -> next = prev -> next;
prev -> next = curr;
于 2012-10-12T15:10:41.657 回答
0

编译器在此行中将您的 usingitemtheData作为数据成员引用:

    curr -> item = item;

您可能的意思是:

    curr -> data = item;

这行:

theData = curr -> theData;

您可能的意思是:

    curr -> data = theData;
于 2012-10-12T15:08:39.407 回答