operator overloading - C++ error: passing xxx as 'this' argument of xxx discards qualifiers -
this question has answer here:
- discards qualifiers error 4 answers
this code i've written access matrix in class , compare 2 objects of same class using less operator , equal operator. compiler throws errors.
#include <bits/stdc++.h> using namespace std; class node { private: int a[5][5]; public: int& operator()(int i, int j) { return a[i][j]; } friend bool operator==(const node& one, const node& two); friend bool operator<(const node& one, const node& two); }; bool operator==(const node& one, const node& two) { (int = 1; < 5; i++) { (int j = 1; j < 5; j++) { if (one(i, j) != two(i, j)) { return false; } } } return true; } bool operator<(const node& one, const node& two) { (int = 1; < 5; i++) { (int j = 1; j < 5; j++) { if (one(i, j) > two(i, j)) { return false; } } } return true; } int main() { node src; (int = 1; < 5; i++) { (int j = 1; j < 5; j++) { cin >> src(i, j); } } return 0; }
the compile time error is:
code.cpp: in function 'bool operator==(const node&, const node&)': code.cpp:20:19: error: passing 'const node' 'this' argument of 'int& node::operator()(int, int)' discards qualifiers [-fpermissive] if (one(i, j) != two(i, j)) { ^ code.cpp:20:32: error: passing 'const node' 'this' argument of 'int& node::operator()(int, int)' discards qualifiers [-fpermissive] if (one(i, j) != two(i, j)) { ^ code.cpp: in function 'bool operator<(const node&, const node&)': code.cpp:31:19: error: passing 'const node' 'this' argument of 'int& node::operator()(int, int)' discards qualifiers [-fpermissive] if (one(i, j) > two(i, j)) { ^ code.cpp:31:31: error: passing 'const node' 'this' argument of 'int& node::operator()(int, int)' discards qualifiers [-fpermissive] if (one(i, j) > two(i, j)) {
can tell me going wrong?
add
const int operator()(int i, int j) const { return a[i][j]; }
Comments
Post a Comment