I have the following function:
#include <cstdint>
using int_32 = int32_t;
bool func(const Coord<int_32> c)
{
for (Coord<int_32> i : boxes)
if (c == i)
return 0;
return 1;
}
Coord<int_32>
is a struct with two members of type int ( int32_t ).
Here is its overloaded == operator:
bool operator == (const Coord<T> &p)
{
if (this -> x == p.x &&
this -> y == p.y)
return 1;
return 0;
}
It gives me an error at if (c == i)
:
error: passing 'const Coord<int>' as 'this' argument discards qualifiers [-fpermissive]
In your comparison:
if (c == i)
the variable c
is const
. However, your operator==
is not const-qualified , so the left hand side of ==
is required to be non-const.
The correct fix for this is to mark operator==
as const
if it's a member function:
bool operator == (const Coord<T> &p) const {
// ^^^^^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.