私の2つの機能に関して小さな問題があります。私のコンパイラは、渡された関数が宣言されていないと言い続けています。コンパイラの内容は次のとおりです。
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