【问题标题】:how to add three objects of same class in c++? [duplicate]如何在 C++ 中添加三个相同类的对象? [复制]
【发布时间】:2018-04-04 19:26:44
【问题描述】:

我可以在 C++ 中使用运算符重载添加三个对象吗??

#include <iostream>
#include <conio.h>
using namespace std;

class complex{
private:
    int a;
public:
    void setdata(int x){
        a=x;
    }
    void showdata(){
        cout<<"\n"<<a;
    }
    int operator +(complex c,complex d){
        complex temp;
        temp.a=a+c.a+c+d.a;
        return (temp);
    }
};

int main(){
complex c1,c2,c3,c4;
c1.setdata(3);
c2.setdata(5);
c4.setdata(2);
c4=c1+c2+c3;
c4.showdata();
}

我正在使用这种方法,但它不起作用请帮助。

【问题讨论】:

  • 是的,你可以。你需要解释什么不起作用。
  • 您的运算符的返回类型错误。
  • 您的complex::operator+() 返回int。这就是为什么 c1+c2 是复数 x 复数 -> int 并且因此 (c1+c2)+c3 是 int x 复数 -> ???。要么将complex::operator+() 的返回值更改为complex,要么需要第二个operator+(int, complex)。这两个选项都应该有效。
  • 我不太确定语法。 AFAIK,您可以将 operator+ 定义为 member 但在这种情况下,this 是第一个参数,您只需将第二个参数定义为参数。或者您将 operator+ 定义为 non-member 并使用示例中的签名。在后一种情况下,您可以将其设置为 complexfriend 以允许其访问 private 成员变量。
  • stackoverflow.com/q/46843462/3754223 和我的回答。看来你们在同一门课上。 :D

标签: c++


【解决方案1】:

您必须更改一点操作符,并且您在变量的初始化中出现错误(C3 未初始化)。 您不必定义同时处理三个术语的运算符。总和将分为两部分 (c1 + c2) + c3;第一个总和返回一个添加到 c3 的“复杂”项目。最后一个和的结果分配给 c4。 请参阅下面的代码。

#include <iostream>
#include <conio.h>
using namespace std;

class complex{
private:
    int a;
public:
    void setdata(int x){
        a = x;
    }
    void showdata(){
        cout << "\n" << a;
    }
    complex operator +(complex c){
        complex temp;
        int i = 0;
        i = a + c.a;
        temp.setdata(i);
        return temp;
    }
};

int main(){
    complex c1, c2, c3, c4;
    c1.setdata(3);
    c2.setdata(5);
    c3.setdata(2);
    c4 = c1 + c2 + c3;
    c4.showdata();
}

【讨论】:

  • 非常感谢兄弟,这是我关于 stack over flow 的第一篇文章 :-)
  • operator+ 应通过 const 引用获取 c 以避免复制。并且它本身应该被声明为const,因为它不会修改*thiscomplex operator+(const complex &amp;c) const
【解决方案2】:

在我的 cmets 中,我提出了两种替代解决方案,但在摆弄示例代码时,我什至找到了第三种解决方案。

示例代码:

#include <iostream>

// Version 1: (return type fixed)

class Complex1 {
  friend std::ostream& operator << (std::ostream &out, const Complex1 &c);
  private:
    int a;
  public:
    explicit Complex1(int a): a(a) { }
    // operator + as member
    Complex1 operator + (const Complex1 &c) const
    {
      return Complex1(a + c.a);
    }
};

std::ostream& operator << (std::ostream &out, const Complex1 &c)
{
  return out << c.a;
}

// Version 2: (two overloaded operators)

class Complex2 {
  friend std::ostream& operator << (std::ostream &out, const Complex2 &c);
  friend int operator+(const Complex2 &c, const Complex2 &d);
  friend int operator+(int c, const Complex2 &d);
  private:
    int a;
  public:
    explicit Complex2(int a): a(a) { }

};

std::ostream& operator << (std::ostream &out, const Complex2 &c)
{
  return out << c.a;
}

int operator+(const Complex2 &c, const Complex2 &d)
{
  return c.a + d.a;
}

int operator+(int c, const Complex2 &d)
{
  return c + d.a;
}

// Version 3: (implicit conversion with constructor)

class Complex3 {
  friend std::ostream& operator << (std::ostream &out, const Complex3 &c);
  private:
    int a;
  public:
    Complex3(int a): a(a) { }
    // operator + as member
    int operator+(const Complex3 &c) const
    {
      return a + c.a;
    }
};

std::ostream& operator << (std::ostream &out, const Complex3 &c)
{
  return out << c.a;
}

// Check everything out:

using namespace std;

int main()
{
  cout << "Version 1:" << endl;
  { Complex1 c1(3), c2(5), c3(2);
    Complex1 c4 = c1 + c2 + c3;
    cout << "c4: " << c4 << endl;
  }
  cout << "Version 2:" << endl;
  { Complex2 c1(3), c2(5), c3(2);
    Complex2 c4 = Complex2(c1 + c2 + c3);
    cout << "c4: " << c4 << endl;
  }
  cout << "Version 3:" << endl;
  { Complex1 c1(3), c2(5), c3(2);
    Complex1 c4 = c1 + c2 + c3;
    cout << "c4: " << c4 << endl;
  }
  cout << "done." << endl;
  return 0;
}

您可以在ideone 上编译和运行代码。


Complex1 提供固定运算符作为成员

Complex1 Complex1::operator + (const Complex1 &c) const;

因此,(c1 + c2) + c3 是

(Complex1 × Complex1 → Complex1) &times Complex1 → Complex1


Complex2 提供两个重载运算符作为非成员

  int operator+(const Complex2 &c, const Complex2 &d);
  int operator+(int c, const Complex2 &d);

因此,c1 + c2 是

Complex2 × Complex2 → int

和 (c1 + c2) + c3 是

int × Complex2 → int


Complex3 与原始示例非常相似,主要区别在于我提供了一个接受int 的非explicit 构造函数。这意味着编译器将在必要时将其用作转换运算符。

(使用适当的构造函数,可能不会很快注意到运算符问题。)

因此,c1 + c2 是

Complex3 × Complex3 → int

和 (c1 + c2) + c3 是

(int → Complex3) × Complex3 → int

【讨论】:

    猜你喜欢
    • 2015-12-16
    • 2018-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多