我可以在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();
}
我正在使用这种方法,但它无法正常工作请帮助。
答案 0 :(得分:1)
你必须改变一点运算符,你在变量的初始化中有一个错误(C3没有被初始化)。 您不必定义同时使用三个术语的运算符。总和将分为两部分(c1 + c2)+ c3;第一笔金额返回一个复杂的&#39;添加到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();
}
答案 1 :(得分:0)
在我的评论中,我提出了两种替代解决方案,但在摆弄示例代码时,我甚至找到了第三种解决方案。
示例代码:
#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)&amp; 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
与原始示例非常相似,但我提供了一个接受explicit
的非int
构造函数。这意味着编译器将在必要时将其用作转换运算符。
(使用适当的构造函数,可能很快就没有注意到运算符问题。)
因此,c1 + c2是
Complex3×Complex3→int
和(c1 + c2)+ c3是
(int→Complex3)×Complex3→int