gpt4 book ai didi

c++ - 编译器说通过变量未声明

转载 作者:行者123 更新时间:2023-11-28 03:30:01 27 4
gpt4 key购买 nike

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

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

最佳答案

错误信息说明了一切。

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

curr -> item = item;

ListNode 没有这样的成员,可能你的意思是 data 而不是 item

错误的另一行也是如此

theData = curr -> theData;

关于c++ - 编译器说通过变量未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12861832/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com