This is the code I've written to access a matrix in a class and compare two objects of the same class using less than operator and equal to operator. But the 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) {
for (int i = 1; i < 5; i++) {
for (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) {
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
if (one(i, j) > two(i, j)) {
return false;
}
}
}
return true;
}
int main() {
node src;
for (int i = 1; i < 5; i++) {
for (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' as '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' as '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' as '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' as 'this' argument of 'int& node::operator()(int, int)' discards qualifiers [-fpermissive]
if (one(i, j) > two(i, j)) {
Can someone tell me where am I going wrong?