我正在处理链表,但无法在常量函数“void Print() const”中修改当前指针的值。
在Print函数中,我想执行“current = head”,然后像“current = current->link”一样递增,但不能这样做,因为它显示:
“error C3490:'current' 无法修改,因为它是通过const对象访问的 e:\Cpp\projects\data structure ass-1\data structure ass-1\source.cpp 83 1 Data Structure Ass-1”
在Print函数中,我想执行“current = head”,然后像“current = current->link”一样递增,但不能这样做,因为它显示:
“error C3490:'current' 无法修改,因为它是通过const对象访问的 e:\Cpp\projects\data structure ass-1\data structure ass-1\source.cpp 83 1 Data Structure Ass-1”
#include<iostream>
struct node
{
int data;
node *link;
};
class List
{
node *head,*current,*last;
public:
List();
// List(const List&);
// ~List();
void print() const;
};
using namespace std;
int main()
{
List List1;
}
void List::print() const
{
current=head; //here is my error
current=current->link;
}
List::List():current(head)
{
}
mutable
,以便在const
函数中进行修改。 - Samprint
函数中创建一个临时的本地变量node*
类型,而不是使用current
作为成员。 - Sam