C++ 运算符重载+;添加两个对象

C++ 运算符重载+;添加两个对象,c++,operator-overloading,C++,Operator Overloading,我试图添加两个对象,它们在同一个类中 在类的私有部分,我有两个int变量 class One { private: int num1, num2; public: One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object friend bo

我试图添加两个对象,它们在同一个类中

在类的私有部分,我有两个
int
变量

class One {

private:
int num1, num2;
public:

    One operator+=(const One&); // - a member operator that adds another One object - to the current object and returns a copy of the current object
    friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
};

 One operator+(const One&, const One&);// - a non-friend helper operator that adds One objects without changing their values and returns a copy of the resulting One 
我想我不确定我在
opeartor+
上是否有问题

One operator+(const One &a, const One &b){

One c,d,r;

c = a;
d = b;

r += b;
r += a;

return r;
}
我认为上面的代码是错误的,但是我尝试使用像b.num1这样的代码,我得到了编译错误

错误:“int One::num1”是私有的
错误:在此上下文中

我也不能使用b->num1,因为上面的函数不在成员函数部分

error: base operand of '->' has non-pointer type 'const One'
这就是它在
main


Result=LeftObject+RightObject

我不明白为什么
操作符+
出错:

#include <stdio.h>

class One {
    public:
        One(int n1, int n2): num1(n1),num2(n2) {}

    private:
        int num1, num2;
    public:
        One operator+=(const One& o) {
            num1 += o.num1;
            num2 += o.num2;
            return *this;
        }
        friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
        void print() {
            printf("%d,%d\n", num1, num2);
        }
};

One operator+(const One& a, const One& b) {
    One r(0,0);
    r += b;
    r += a;
    return r;
}

int main() {
    One a(1,2),b(3,4);
    One r = a + b;
    r.print();
}
#包括
一班{
公众:
一(intn1,intn2):num1(n1),num2(n2){}
私人:
int num1,num2;
公众:
一个运算符+=(常数一和o){
num1+=o.num1;
num2+=o.num2;
归还*这个;
}
friend bool运算符==(常数一&,常数一&);//-比较两个一类对象是否相等的friend运算符
作废打印(){
printf(“%d,%d\n”,num1,num2);
}
};
一个操作员+(常数一和a、常数一和b){
一个r(0,0);
r+=b;
r+=a;
返回r;
}
int main(){
一个a(1,2),b(3,4);
一个r=a+b;
r、 打印();
}

我不明白为什么
操作符+
是错误的:

#include <stdio.h>

class One {
    public:
        One(int n1, int n2): num1(n1),num2(n2) {}

    private:
        int num1, num2;
    public:
        One operator+=(const One& o) {
            num1 += o.num1;
            num2 += o.num2;
            return *this;
        }
        friend bool operator==(const One&, const One&); // - a friend operator that compares two One class objects for equality
        void print() {
            printf("%d,%d\n", num1, num2);
        }
};

One operator+(const One& a, const One& b) {
    One r(0,0);
    r += b;
    r += a;
    return r;
}

int main() {
    One a(1,2),b(3,4);
    One r = a + b;
    r.print();
}
#包括
一班{
公众:
一(intn1,intn2):num1(n1),num2(n2){}
私人:
int num1,num2;
公众:
一个运算符+=(常数一和o){
num1+=o.num1;
num2+=o.num2;
归还*这个;
}
friend bool运算符==(常数一&,常数一&);//-比较两个一类对象是否相等的friend运算符
作废打印(){
printf(“%d,%d\n”,num1,num2);
}
};
一个操作员+(常数一和a、常数一和b){
一个r(0,0);
r+=b;
r+=a;
返回r;
}
int main(){
一个a(1,2),b(3,4);
一个r=a+b;
r、 打印();
}

如果您已经实现了此成员功能:

One One::operator+=(const One&);
然后,您可以实现非成员添加运算符,如下所示:

One operator+(const One& lhs, const One& rhs) {
  One result = lhs;
  result += rhs;
  return result;
}
这可以简化为以下几点:

One operator+(One lhs, const One& rhs) {
  return lhs += rhs;
}
此模式(您可以对所有运算符/运算符分配对进行调整)将运算符分配版本声明为成员—它可以访问私有成员。它将操作符版本声明为非友元非成员——这允许在操作符的任一侧进行类型升级

旁白:
+=
方法应该返回对
*此
的引用,而不是副本。所以它的声明应该是:
One&operator+(constone&)


编辑:下面是一个工作示例程序

#include <iostream>
class One {
private:
  int num1, num2;

public:
  One(int num1, int num2) : num1(num1), num2(num2) {}
  One& operator += (const One&);
  friend bool operator==(const One&, const One&);
  friend std::ostream& operator<<(std::ostream&, const One&);
};

std::ostream&
operator<<(std::ostream& os, const One& rhs) {
  return os << "(" << rhs.num1 << "@" << rhs.num2 << ")";
}

One& One::operator+=(const One& rhs) {
  num1 += rhs.num1;
  num2 += rhs.num2;
  return *this;
}

One operator+(One lhs, const One &rhs)
{
  return lhs+=rhs;
}

int main () {
  One x(1,2), z(3,4);
  std::cout << x << " + " << z << " => " << (x+z) << "\n";
}
#包括
一班{
私人:
int num1,num2;
公众:
一(intnum1,intnum2):num1(num1),num2(num2){}
一个运算符+=(常数一&);
友元布尔运算符==(常数一&,常数一&);

friend std::ostream&operator如果您已经实现了此成员函数:

One One::operator+=(const One&);
然后,您可以实现非成员添加运算符,如下所示:

One operator+(const One& lhs, const One& rhs) {
  One result = lhs;
  result += rhs;
  return result;
}
这可以简化为以下几点:

One operator+(One lhs, const One& rhs) {
  return lhs += rhs;
}
此模式(可适用于所有运算符/运算符分配对)将运算符分配版本声明为成员—它可以访问私有成员。它将运算符版本声明为非友元非成员—这允许在运算符的任一侧进行类型升级

旁白:
+=
方法应该返回对
*this
的引用,而不是副本。因此它的声明应该是:
One&operator+(const One&)


编辑:下面是一个工作示例程序

#include <iostream>
class One {
private:
  int num1, num2;

public:
  One(int num1, int num2) : num1(num1), num2(num2) {}
  One& operator += (const One&);
  friend bool operator==(const One&, const One&);
  friend std::ostream& operator<<(std::ostream&, const One&);
};

std::ostream&
operator<<(std::ostream& os, const One& rhs) {
  return os << "(" << rhs.num1 << "@" << rhs.num2 << ")";
}

One& One::operator+=(const One& rhs) {
  num1 += rhs.num1;
  num2 += rhs.num2;
  return *this;
}

One operator+(One lhs, const One &rhs)
{
  return lhs+=rhs;
}

int main () {
  One x(1,2), z(3,4);
  std::cout << x << " + " << z << " => " << (x+z) << "\n";
}
#包括
一班{
私人:
int num1,num2;
公众:
一(intnum1,intnum2):num1(num1),num2(num2){}
一个运算符+=(常数一&);
友元布尔运算符==(常数一&,常数一&);

friend std::ostream&operator
运算符+
是否有错误?顺便说一句:您忘记初始化成员。您不能执行
b.num1
num1
是私有成员。如果您需要访问它,您需要有一个getter函数。
b->
无效,因为您没有创建指针类型。是否打算添加private integers?@other.anon.coward我怎么能得到它?我只想找到一种方法,复制两个对象,然后以对象的名称返回它们
r
我不抱歉,我不太明白。要得到private,应该
操作符+
添加什么?创建一个公共函数,返回私有成员-
公共:内联int getInt1(){return int1;}
@another.anon.coward在
操作符+
中,假设将两个对象添加在一起,并将其分配给对象调用
r
,然后将其返回到main中的main函数,该操作符的调用如下
结果=左+右;
操作符+
何时出错?顺便说一句:您忘记初始化成员了。你不能这样做,
b.num1
num1
是一个私有成员。如果你需要访问它,你需要一个getter函数。
b->
无效,因为你没有创建指针类型。你想添加私有整数吗?@other.anon.coward我怎么才能得到它?我只是想找到一种方法来复制两个对象然后以对象名
r
返回它们,我不是很抱歉,我不太明白。应该添加什么
operator+
add?要获得private,请创建一个公共函数,该函数返回私有成员可能-
public:inline int getInt1(){return int1;}
@another.anon.coward在
操作符+
中,假设将两个对象添加在一起,并将其分配给对象调用
r
,然后将其返回到main中的main函数该操作符的调用如下
结果=左+右;
我认为您不应该修改lhsI