I have the following C++ code:
#include <iostream>
#include <vector>
using namespace std;
class A
{
private:
int i;
public:
void f1(const A& o);
void f2()
{
cout<<i<<endl;
}
};
void A::f1(const A& o)
{
o.f2();
}
It just doesn’t compile. Can somebody give a explanation? Thanks!
Presumably, your compiler told you why it doesn’t compile. Mine said:
This tells me that you’re trying to call a non-
constmember function (A::f2) on a reference to aconstobject (const A& o).Either add a
constqualifier to the function to allow it to be called onconstobjects:or remove
constfrom the reference to allow modification – but in this case, don’t do that, sincef2()doesn’t need to modify the object.