0
我正在使用C++中的模板制作自定义列表并获取一些编译错误。
代码的长度非常大,因此这里是错误发生地点的一小段代码。编译错误如下。你可以编译它自己的系统来查看相同的错误。编译器“错误:传递'const something'作为'this'参数丢弃限定符'
#include <iostream>
using namespace std;
template <class T>
class sortedList
{
int m_count;
public:
sortedList(){m_count = 0;}
int length(){ return m_count; }
};
void output(const sortedList<int>& list)
{
cout << "length" << list.length() << endl;
return;
}
int main() {
// your code goes here
sortedList <int> list1;
output(list1);
return 0;
}
我得到的编译错误:
prog.cpp: In function ‘void output(const sortedList<int>&)’:
prog.cpp:17:35: error: passing ‘const sortedList<int>’ as ‘this’ argument discards qualifiers [-fpermissive]
cout << "length" << list.length() << endl;
^
prog.cpp:10:6: note: in call to ‘int sortedList<T>::length() [with T = int]’
int length(){ return m_count; }
要调用非const限定的方法'长度'在const限定对象'list'上。使'length()'const限定。 – VTT
'int length(){return m_count; }' - >'int length()const {return m_count; }' – Yunnosch
你读过错误了吗?你明白这是什么意思? –