c++ - 无法从“常量列表”转换“this”指针'到'列表&'
问题描述
我实现了一个双向链表。然后我将该链表用作堆栈的容器。但我收到错误 C2662 'const T &List::last_elem(void)': cannot convert 'this' pointer from 'const List' 到 'List &'。我试图作为一个值返回,但它没有用。我不明白编译器是否指向错误点。
template<typename T>
struct Node
{
T data;
Node* next;
Node* prev;
Node() :data{ 0 } { next = nullptr; prev = nullptr; }
};
template<typename T>
class List
{
private:
Node<T>* headNode;
Node<T>* tailNode;
public:
List()
{
headNode = new Node<T>;
tailNode = new Node<T>;
headNode->next = tailNode;
tailNode->prev = headNode;
}
~List()
{
Node<T>* current = headNode;
while (current)
{
Node<T>* tempNode = current;
current = current->next;
delete tempNode; cout << "\nDeleting List!!!";
}
}
bool empty()
{
return (headNode->next == tailNode);
}
const T &last_elem()
{
return tailNode->prev->data;
}
const T &first_elem()
{
return headNode->next->data;
}
void remove_first()
{
Node<T>* tempNode = headNode;
headNode = tempNode->next;
delete tempNode; cout << "\nDeleting Node!!!";
headNode->prev = nullptr;
}
void remove_last()
{
Node<T>* tempNode = tailNode;
tailNode = tempNode->prev;
delete tempNode; cout << "\nDeleting Node!!!";
tailNode->next = nullptr;
}
void add_front(T d)
{
headNode->data = d;
Node<T>* tempNode = new Node<T>;
tempNode->next = headNode;
headNode->prev = tempNode;
headNode = tempNode;
}
void add_end(T d)
{
tailNode->data = d;
Node<T>* tempNode = new Node<T>;
tempNode->prev = tailNode;
tailNode->next = tempNode;
tailNode = tempNode;
}
void print_list()
{
Node<T>* current = headNode->next;
while (current)
{
cout << current->data << "|-> ";
current = current->next;
}
}
void reverse_print_list()
{
Node<T>* current = tailNode->prev;
while (current)
{
cout << current->data << "|-> ";
current = current->prev;
}
}
};
template<typename T>
class ListStack
{
private:
List<T> stacklist;
int index;
public:
class StackException
{
private:
string errMessage;
public:
StackException(string err) :errMessage(err) {}
string getErrMessage() { return errMessage; }
};
ListStack():index { -1 }{}
int size() const // number of items in the stack
{
return index + 1;
}
bool empty() const // is the stack empty?
{
return (index == -1);
}
const T& top() const throw(StackException) // the top element
{
if (empty())throw StackException(string("Stack is empty!!!"));
return stacklist.last_elem();
}
void push(const T& e) // push element onto stack
{
stacklist.add_end(e);
++index;
}
void pop() throw(StackException) // pop the stack
{
if (empty())throw StackException(string("Stack is empty!!!"));
stacklist.remove_last();
--index;
}
};
int main()
{
try
{
ListStack<int> myls;
myls.push(5);
myls.push(8);
myls.push(9);
myls.push(12);
myls.push(17);
cout << myls.top() << endl;
myls.pop();
myls.pop();
myls.pop();
myls.pop();
myls.pop();
myls.pop();
myls.pop();
myls.pop();
myls.pop();
}
catch (ListStack<int>::StackException se)
{
cout << se.getErrMessage();
}
return 0;
}
解决方案
问题是:
const T& top() const throw(StackException)
删除第二个 const 到
const T& top() throw(StackException)
你应该没事。
(这是因为被调用的last_elem()
方法不是 const ,因为常量方法显然不能调用非常量方法,所以使一个 const 也可以工作)。
推荐阅读
- apache-flink - Flink 中的树评估
- c# - 如何从 FileStream 读取数据并在中间将它们添加到队列中
- amazon-web-services - 如何强制网络抓取脚本根据我的国家而不是 AWS 的服务器位置获取结果?
- git - 本地分支的 Git 提交到 repo 也会复制主文件
- vue.js - 需要在 AG-Grid 过滤器中使用空格分隔值来返回每个匹配项
- amazon-web-services - 使用 AWS_CLI 执行 aws iam list-users 时出错
- javascript - 将字符串数字格式化为十进制精度或 web3
- javascript - 从角度画布在浏览器窗口外绘图?
- reactjs - 如何避免在最高级别的 React 组件中包含所有方法?
- javascript - Jquery,错误发生未捕获类型错误:无法读取 null 的属性“defaultView”