簡體   English   中英

錯誤:將 'const std::__cxx11::list<>' 作為 'this' 參數傳遞會丟棄限定符

[英]error: passing ‘const std::__cxx11::list<>’ as ‘this’ argument discards qualifiers

我有一個小問題,找不到解決方法。。

在 my.hpp 文件中,我聲明了這兩個函數和結構,但收到錯誤“將 'const std::__cxx11::list<>' 作為 'this' 參數丟棄限定符。”

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;
};

我試圖做的 in.cpp 文件:

void StudentRegistry::Add(const Student &t){
    this->GetStudents().push_back(t);
}

const std::list<Student>& StudentRegistry::GetStudents() const{
    return students;
}

我怎樣才能使這項工作?

const std::list<Student>& GetStudents() const;

this->GetStudents().push_back(t);

有沖突。 您有一個修改從GetStudents()獲得的列表的合同,但您嘗試使用push_back來做到這一點。

解決方案:

void StudentRegistry::Add(const Student& t) {
    students.push_back(t);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM