【发布时间】:2021-03-23 09:53:58
【问题描述】:
#include <iostream>
#include <set>
using namespace std;
class StudentT {
public:
int id;
string name;
public:
StudentT(int _id, string _name) : id(_id), name(_name) {
}
int getId() {
return id;
}
string getName() {
return name;
}
};
inline bool operator< (StudentT s1, StudentT s2) {
return s1.getId() < s2.getId();
}
int main() {
set<StudentT> st;
StudentT s1(0, "Tom");
StudentT s2(1, "Tim");
st.insert(s1);
st.insert(s2);
set<StudentT> :: iterator itr;
for (itr = st.begin(); itr != st.end(); itr++) {
cout << itr->getId() << " " << itr->getName() << endl;
}
return 0;
}
排队:
cout << itr->getId() << " " << itr->getName() << endl;
它给出一个错误:
../main.cpp:35: 错误:将 'const StudentT' 作为 'int StudentT::getId()' 的 'this' 参数传递会丢弃限定符
../main.cpp:35: 错误:将 'const StudentT' 作为 'std::string StudentT::getName()' 的 'this' 参数传递会丢弃限定符
这段代码有什么问题?谢谢!
【问题讨论】:
-
代码 sn-p 中的第 35 行在哪里?
-
我希望 GCC 能改进这个错误信息,例如“丢弃限定符”->“破坏 const 正确性”
-
@jfritz42:对于它丢弃
volatile的情况会令人困惑 -
@PlasmaHH 错误消息将分为“破坏 const 正确性”和“破坏 volatile 正确性”。现在,没有多少人会考虑 volatile 正确
标签: c++