【发布时间】:2014-10-16 07:14:23
【问题描述】:
我有一个 A 类,
class A{
private:
int num;
public:
A(int n){ num = n; };
int getNum(){
return num;
}
A operator+(const A &other){
int newNum = num + other.getNum();
return A(newNum);
};
};
为什么other.getNum() 会报错?我可以很好地访问其他 (other.num) 中的变量,但似乎我永远无法使用其他任何函数。
我得到的错误类似于
无效参数:候选者是 int getNum()。
我可以写int test = getNum(),但不能写int test = other.getNum(),但我几乎可以肯定我能够以某种方式调用other.getNum()。
我是否忽略了什么?
【问题讨论】:
-
other是常量引用,而getNum不是常量成员函数。
标签: c++ class overloading