C++ 编译器说传递的变量是未声明的

C++ 编译器说传递的变量是未声明的,c++,C++,我有一个关于我的两个函数的小问题。我的编译器一直说传递的函数是未声明的。下面是编译器所说的: 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 ListNod

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

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'
LinkedList.hpp:在成员函数“void LinkedList::insert(int,T)[带T=std::basic_字符串]”中:
test.cpp:92:1:从此处实例化
LinkedList.hpp:96:2:错误:“struct ListNode”没有名为“item”的成员
在test.cpp:4:0中包含的文件中:
LinkedList.hpp:在成员函数“T LinkedList::remove(int)[带T=std::basic_字符串]”中:
test.cpp:92:1:从此处实例化
LinkedList.hpp:139:2:错误:“struct ListNode”没有名为“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
\ifndef\u CS235\u LINKEDLIST\u H_
#定义_CS235_链接列表_H_
#定义列表(最多10个)
#包括“ABCList.hpp”
#包括
使用名称空间std;
模板
结构列表节点
{
T data;//列表项
ListNode*next;//指向下一个节点的指针
};
模板
类链接列表:public ABCList{
私人:
整数大小;
ListNode*头;
ListNode*查找(int pos);
公众:
LinkedList();
LinkedList(LinkedList&其他);
虚拟~LinkedList();
虚拟布尔是空的();
虚拟int getLength();
虚拟空位插入(int pos,T项目);
虚拟T移除(int-pos);
虚拟T检索(int-pos);
};
模板
LinkedList::LinkedList()//默认构造函数
{
尺寸=0;
head=NULL;
}
模板
LinkedList::LinkedList(LinkedList和其他)//复制构造函数
{
for(int i=1;inext;}
返回温度;
} 
}
模板
bool LinkedList::isEmpty()//函数检查列表是否为空
{
如果(大小==0)
返回true;
返回false;
}
模板
int LinkedList::getLength()//返回大小的函数。
{
返回大小;
}
模板
void LinkedList::insert(int pos,T item)//此函数用于插入一个项。
{
ListNode*curr=new ListNode();//将ListNode插入到新创建的节点中
当前->项目=项目;
如果(位置==1)
{//节点被添加到开头
当前->下一步=头部;
水头=电流;
}
其他的
{
ListNode*prev=find(pos-1);//在prev之后放置一个新节点
当前->下一步=上一步;
上一个->下一个=当前;
++大小;
}
}
模板
T LinkedList::remove(int pos)//此函数删除一个项
{
尝试
{
if(pos<1 | | pos>getLength()+1)//如果列表的位置大于或等于列表的位置,则会抛出错误。
掷99;
}
捕获(整数x)
{cout next;//节点保存到指针
上一个->下一个=当前->下一个;
}
--大小;
数据=当前->数据;
删除curr;
返回数据;
}
模板
T LinkedList::检索(int pos)
{
尝试
{
如果(pos<1)//如果位置小于1,则会发生错误。
掷99;
}
捕获(整数x)
{cout data;//将数据返回到节点。
}
}
#恩迪夫

错误消息说明了一切

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

curr -> item = item;
ListNode
没有这样的成员,可能您指的是
数据
,而不是

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

theData = curr -> theData;

编译器引用您使用
数据
作为此行的数据成员:

    curr -> item = item;
theData = curr -> theData;
你的意思可能是:

    curr -> data = item;
    curr -> data = theData;
这一行:

    curr -> item = item;
theData = curr -> theData;
你的意思可能是:

    curr -> data = item;
    curr -> data = theData;

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

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

您在函数remove中使用line
curr->the data
无效,原因与此相同

同时检查您的插入

我相信密码

curr ->  next = prev;
prev -> next = curr;
将使
curr
指向
prev
,并将
prev
指向
curr
?!闭环?!这就是你想要的吗

你是说

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

我没有看到任何类包含编译器抱怨的成员,更不用说
ListNode
<如果类型
curr
不包含名为
item
的成员,则code>curr->item无法工作,对于
data
也是如此。编程的一个重要技巧(尤其是在C++中)是学习读取错误消息。努力阅读完整的错误消息并理解编译器试图告诉您的内容。因此,我还必须作废LinkedList::insert(int pos,T item)以作废LinkedList::insert(int pos,ListNode item)否,您需要替换
curr->item=item
当前->数据=项目。不知道为什么这并不明显。与
theData=curr->theData相同应该是
theData=curr->data