我遇到一个关于我的两个功能的小问题。我的编译器继续说传递的函数是未声明的。下面是编译器所说的内容:
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
答案 0 :(得分:2)
错误消息说明了一切。
导致错误的这一行访问item
struct
ListNode
成员
curr -> item = item;
ListNode
没有这样的成员,可能你的意思是data
而不是item
。
这同样适用于另一行错误
theData = curr -> theData;
答案 1 :(得分:0)
编译器指的是您使用item
和theData
作为此行中的数据成员:
curr -> item = item;
你可能意味着:
curr -> data = item;
和这一行:
theData = curr -> theData;
你可能意味着:
curr -> data = theData;
答案 2 :(得分:0)
嗯..编译器告诉你,根据你在错误中所说的话
您在行curr -> item
中对行insert
的使用无效,因为该结构没有名为item
的成员,其成员名为data
由于同样的原因,你在函数remove中使用行curr -> theData
无效...
同时检查您的插入内容,
我相信代码
curr -> next = prev;
prev -> next = curr;
将curr
指向prev
,将prev
指向curr
?关闭循环?!那是你要的吗?!
你不是说
curr -> next = prev -> next;
prev -> next = curr;