使用对象的构造函数时,参数会舍弃限定符

时间:2019-06-11 04:48:33

标签: c++ operator-overloading qualifiers

在我的C ++项目中实施“超越生命的游戏”算法时,在编译时遇到错误:

在/ usr / include / c ++ / 7 / algorithm:62:0包含的文件中,                  从src / ZPR-大于life / state.cpp:8: /usr/include/c++/7/bits/stl_algo.h:实例化'_ForwardIterator std :: __ remove_if(_ForwardIterator,_ForwardIterator,_Predicate)[with _ForwardIterator = std :: _ Rb_tree_const_iterator; _Predicate = __gnu_cxx :: __ ops :: __ Iter_equals_val]': /usr/include/c++/7/bits/stl_algo.h:906:30:从'_FIter std :: remove(_FIter,_FIter,const _Tp&)[带有_FIter = std :: _ Rb_tree_const_iterator; _Tp =单元格]' src / ZPR-大于寿命/state.cpp:31:85:从此处开始 /usr/include/c++/7/bits/stl_algo.h:871:16:错误:将“ const Cell”作为“ this”参数传递会丢弃限定符[-fpermissive]        __ result = _GLIBCXX_MOVE( __ first);                 ^ 在src / ZPR-large-than-life / state.cpp:6:0中包含的文件中: src / ZPR-大于寿命/cell.hpp:6:7:注意:在调用“ Cell&Cell :: operator =(const Cell&)”时  细胞类{        ^ ~~~

使用了Cell类中的重载=的多个版本,似乎都无法正常工作和正确编译(以“常规”方式重载该运算符会使编译器无法识别它)。构造函数Cell()设置为默认值

状态类的声明:

class State{
    std::set<Cell> active_cells;
    std::set<Cell> inactive_cells;
    int it_number;
public:
    State();
    ~State();
    State(std::set<Cell>, std::set<Cell>, int);

    void addActiveCell(Cell);
    void removeActiveCell(Cell);
    void addInactiveCell(Cell);
    void removeInactiveCell(Cell);

    std::set<Cell>  getActiveCells();
    std::set<Cell>  getInactiveCells();
    int getItNumber();
    void setActiveCells(std::set<Cell>);
    void setInactiveCells(std::set<Cell>);
    void setItNumber(int it);
};

removeActiveCell函数的实现(在编译过程中抛出错误):

void State::removeActiveCell(Cell cell){      active_cells.erase(std::remove(active_cells.begin(),active_cells.end(), cell),active_cells.end());
}

以及Cell类中构造函数的实现

Cell::Cell(int x, int y, int st){
    coords = std::make_pair(x,y);
    state = st;
}

坐标和状态是此类中的字段

0 个答案:

没有答案