【发布时间】:2010-03-04 21:26:08
【问题描述】:
对于我的 compsci 类,我正在实现一个 Stack 模板类,但遇到了一个奇怪的错误:
Stack.h:在成员函数'
const T Stack<T>::top() const[with T = int]'中:Stack.cpp:10: 错误:将“
const Stack<int>”作为“void Stack<T>::checkElements()[with T = int]”的“this”参数传递会丢弃限定符
Stack<T>::top() 看起来像这样:
const T top() const {
checkElements();
return (const T)(first_->data);
}
Stack<T>::checkElements() 看起来像这样:
void checkElements() {
if (first_==NULL || size_==0)
throw range_error("There are no elements in the stack.");
}
堆栈使用链接节点进行存储,因此first_ 是指向第一个节点的指针。
为什么会出现这个错误?
【问题讨论】:
标签: c++