error: passing ‘const …'’ as ‘this’ argument of ‘…’ discards qualifiers

Hello, I am making a C++ program. I have finished. but the compiler returns me this error:
decharge.cpp:145:24: error: passing ‘const Triangle’ as ‘this’ argument of ‘const Point& Triangle::operator[](size_t)’ discards qualifiers [-fpermissive]

I make a struct Point with two double numbers and an other struct Triangle with an array of Points and the override the operator [].

struct Point {
double x;
double y;
};

typedef vector<Point> Commune;

struct Triangle {
Point a[3];
const Point& operator[](size_t i){return a[i]; }
};

The trouble appears in this function:

void affiche(Triangle const& t)
{
affiche(Commune({ t[0], t[1], t[2] }));
}

The problem is I mustn't modify the affiche function. How can I fix it?

Thanks a lot.
Last edited on
Change your operator[] function to :

const Point& operator[](size_t i) const {return a[i]; }

By the way, when you are posting code, please use the code tags. Select the Format button that looks like "<>", and paste your code between the 2 tags that it generates.
I can fix it! Your assistance was very useful. I am going to follow your advise about the code tags.

Thanks a lot.
Topic archived. No new replies allowed.