Đối với lớp compsci của tôi, tôi đang thực hiện một Stack mẫu lớp, nhưng đã chạy vào một lỗi lẻ:loại bỏ vòng loại lỗi
Stack.h: In member function ‘
const T Stack<T>::top() const
[with T = int]’:Stack.cpp:10: error: passing ‘
const Stack<int>
’ as ‘this
’ argument of ‘void Stack<T>::checkElements()
[with T = int]’ discards qualifiers
Stack<T>::top()
trông như thế này:
const T top() const {
checkElements();
return (const T)(first_->data);
}
Stack<T>::checkElements()
trông như thế này :
void checkElements() {
if (first_==NULL || size_==0)
throw range_error("There are no elements in the stack.");
}
Ngăn xếp sử dụng nút được liên kết để lưu trữ, vì vậy first_
là một con trỏ đến nút đầu tiên.
Tại sao tôi gặp phải lỗi này?