为什么这么写没有输出
#include <iostream>
using namespace std;
class person{
public:
int m_a;
int m_b;
person operator+(person &p){
person temp;
temp.m_a=this->m_a+p.m_a;
temp.m_b=this->m_b+p.m_b;
return temp;
}
};
void test01(){
person p1;
p1.m_a=10;
p1.m_b=20;
person p2;
p2.m_a=10;
p2.m_b=20;
person p3=p1+p2;
cout<<p3.m_a<<endl;
cout<<p3.m_b<<endl;
}
int main()
{
void test01();
return 0;
}
1.28行,应该是test01()
2. 全局函数重载也给你补上了
3.class 名最好是首字母大写
如有帮助,请采纳。
#include <iostream>
using namespace std;
class Person{
public:
int m_a;
int m_b;
/*1 成员函数重载+号
Person operator+(Person &p){
Person temp;
temp.m_a=this->m_a+p.m_a;
temp.m_b=this->m_b+p.m_b;
return temp;
}*/
};
//2 全局函数重载+号
Person operator+(Person p1,Person p2)
{
Person temp;
temp.m_a = p1.m_a + p2.m_a;
temp.m_b = p1.m_b + p2.m_b;
return temp;
}
void test01(){
Person p1;
p1.m_a = 22;
p1.m_b = 22;
Person p2;
p2.m_a = 10;
p2.m_b = 10;
//1.成员函数重载本质调用
//Person p3 = p1.operator+(p2);
//2.全局函数重载本质调用
//Person p3 = operator+(p1,p2);
Person p3 = p1 + p2;
cout << " p3.m_a = "<< p3.m_a << endl;
cout << " p3.m_b = "<< p3.m_b << endl;
}
int main()
{
test01();
return 0;
}