错误:将"const std :: __ cxx11 :: list<>"作为“此"参数传递会丢弃限定符 [英] error: passing ‘const std::__cxx11::list<>’ as ‘this’ argument discards qualifiers
本文介绍了错误:将"const std :: __ cxx11 :: list<>"作为“此"参数传递会丢弃限定符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个小问题,找不到解决方法.
I have a little problem and can't find a way to fix it..
在我的.hpp文件中,我声明了这两个函数和struct,但是由于此"参数放弃了限定词,因此出现了通过const std :: __ cxx11 :: list<>传递错误"的错误.
In my .hpp file I have declared these two functions and struct but getting the error 'passing ‘const std::__cxx11::list<>’ as ‘this’ argument discards qualifiers.'
struct Student {
std::string name;
std::string student_id;
};
class StudentRegistry {
public:
StudentRegistry(){}
void Add(const Student &t);
const std::list<Student>& GetStudents() const;
private:
std::list<Student> students;
};
在.cpp文件中,我尝试执行以下操作:
And in .cpp file I have tried to do:
void StudentRegistry::Add(const Student &t){
this->GetStudents().push_back(t);
}
const std::list<Student>& StudentRegistry::GetStudents() const{
return students;
}
我该如何进行这项工作?
How can I make this work?
推荐答案
const std::list<Student>& GetStudents() const;
和
this->GetStudents().push_back(t);
有冲突.您有不修改从 GetStudents()
中获得的列表的合同,但是您尝试使用 push_back
来做到这一点.
are in conflict. You have a contract to not modify the list you get from GetStudents()
, but you try to do that with push_back
.
解决方案:
void StudentRegistry::Add(const Student& t) {
students.push_back(t);
}
这篇关于错误:将"const std :: __ cxx11 :: list<>"作为“此"参数传递会丢弃限定符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文