I am not able to set value to a private member variable through set method. Getting error
member function 'setCost' not viable: 'this' argument has type 'const value_type' (aka 'const Position'), but function is not marked const
I have below code:
class Position {
public:
Position();
Position(int x, int y);
int getCost() const;
void setCost (int c);
private:
int x;
int y;
int cost;
};
void Position::setCost (int c){
this->cost = c;
}
class Board{
public:
Board();
Board(int N);
void shortestPath32 (Position start, Position end);
private:
int N;
char W[32][32];
};
void Board::shortestPath32 (Position start, Position end){
/* some code here */
set <Position> validMoves = getValidPositions(parent);
for(auto child =validMoves.begin(); child!=validMoves.end(); ++child ){
/*some code here ...*/
int c = 5
(*child).setCost(c);
}
}
}
Clearly if I declare setCost as void Position::setCost (int c) const
, I am not able to do the assignment operation inside. Also, I looked into this thread for set method but wasn't helpful.