求助:在定义类的对象时,为什么在后面加了一个空括号就不对了呢?
不加括号的代码:#include<iostream>
using namespace std;
class FS
{
private:
float sb;
float xb;
public:
FS()
{
}
FS (float i,float j)
{
sb=i;
xb=j;
}
FS operator + (FS one )
{
return (FS(sb+one.sb,xb=one.xb));
}
void show()
{
cout<<sb<<'\t'<<xb<<"i"<<endl;
}
};
void main()
{
FS one(1.2,3.5);
FS two(2.4,-1.1);
FS three;
three = one +two;
three.show();
}
加了括号后的代码:
#include<iostream>
using namespace std;
class FS
{
private:
float sb;
float xb;
public:
FS()
{
}
FS (float i,float j)
{
sb=i;
xb=j;
}
FS operator + (FS one )
{
return (FS(sb+one.sb,xb=one.xb));
}
void show()
{
cout<<sb<<'\t'<<xb<<"i"<<endl;
}
};
void main()
{
FS one(1.2,3.5);
FS two(2.4,-1.1);
FS three(); //加了一个空括号
three = one +two;
three.show();
}
报错为:
F:\C++\10.cpp(32) : error C2659: '=' : overloaded function as left operand
F:\C++\10.cpp(33) : error C2228: left of '.show' must have class/struct/union type