我有一个类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'不是一个常量成员函数。 – Claudio 2014-10-16 07:37:33
推荐阅读:** [我应该在哪个计算机科学/编程协议栈中发布?](http://meta.stackexchange.com/a/129632/165773)** – gnat 2014-10-16 07:40:45