去掉 &
改成(const Integer opr)
反正你只要他的值,用不着改它

解决方案 »

  1.   

    去掉&还是不行,还是同样的提示!
      

  2.   

    去掉const是可以的,但我不想去掉它,能不能有其它办法?
      

  3.   

    jw_nj() :
       你去掉debug文件夹,再重新编译!
      

  4.   

    const Integer &opr
    这样的参数格式好象只能用于“拷贝构造函数”
      

  5.   

    int GetObjectValue(const Integer *opr) { return opr->GetValue(); }
        
      

  6.   

    return opr.GetValue(); 
    是不是opr->GetValue();
      

  7.   

    "去掉debug文件夹,再重新编译"也不行.请问这到底是什么原因?
      

  8.   

    改成
    int GetObjectValue(const Integer *opr) { return opr->GetValue(); }
    也不行.
    我希望函数声明保持不变,而采用其它办法?
      

  9.   

    "去掉debug文件夹,再重新编译"
    这时的错误提示是什么?
      

  10.   

    class Integer
    {
    public:
        int GetValue() const { return m_i;}
        int GetObjectValue(const Integer& opr) { return opr.GetValue(); }
        
    private:
        int m_i;
    };
    我试了一下这样可以
      

  11.   

    这里就不应该使用const,因为在GetObjectValue函数内部,你调用了Integer类的成员函数,而该函数无法保证不修改Integer类的成员变量(虽然确实没有修改)如果你却想加const,那么你必须在Integer中重载const操作符,告诉编译器如何进行const转换,然后在GetObjectValue中用const_cast进行转换。
      

  12.   

    class Integer
    {
    public:
        int GetValue() const { return m_i;}
        int GetObjectValue(const Integer& opr) { return opr.GetValue(); }
        
    private:
        int m_i;
    };
    我试了一下这样可以
      

  13.   

    "去掉debug文件夹,再重新编译"
    这时的错误提示还是和原来一样的.
      

  14.   

    多谢大家,特别是:
    wabc(wabc)
    qunta(旺财1860)
      

  15.   

    int GetValue()const
    {
     return m_i;
    }
    请把成员函数 GetValue()声明为const函数,
    因为参数opr是const的,它只能调用也声明为const的函数.
    原因很简单,因为opr是const的,它不会也不能够改变它所引用的对象,
    所以opr所调用的函数GetValue()也不能改变opr引用的对象,
    但是如果不把GetValue()声明为const就不能保证这个函数不会改变opr所引用的对象.
    --------------
    去掉const是可以的,但我不想去掉它,能不能有其它办法?
    --------------
    你的这个尝试正说明了这一点.
      

  16.   

    去掉.opr可以运行,不过不知是不是你想要的^-^