无法将'this'指针转换为Class&

Dre*_*rew 4 c++

有人能说出为什么我在编写这门课时会遇到这个错误吗?

class C
{
public:
    void func(const C &obj)
    {
       //body
    }

private:
    int x;
};

void func2(const C &obj)
{
    obj.func(obj);
}

int main() { /*no code here yet*/}
Run Code Online (Sandbox Code Playgroud)

Han*_*ant 11

C :: func()方法不承诺它不会修改对象,它只承诺它不会修改它的参数.固定:

   void func(const C &obj) const
    {
       // don't change any this members or the compiler complains
    }
Run Code Online (Sandbox Code Playgroud)

或者使它成为静态功能.当C对象作为参数时,它确实听起来应该是这样的.