新手求教 不是考试 不是作业
#include<iostream>
using namespace std;
class A
{
private:
double x;
double y;
public:
A(double a=0,double b=0)
{
x=a;
y=b;
}
operator*(A ta1,A ta2);
operator/(A ta1,A ta2);
};
A::operator *(A ta1,A ta2)
{
A temp;
temp.x=ta1.x*ta2.x;
temp.y=ta1.y*ta2.y;
cout<<temp.x<<endl<<temp.y<<endl;
return 0;
}
A::operator /(A ta1,A ta2)
{
A temp;
temp.x=ta1.x/ta2.x;
temp.y=ta1.y/ta2.y;
cout<<temp.x<<endl<<temp.y<<endl;
return 0;
}
void main()
{
A t1(1,2),t2(3,4),t3;
t3=t1*t2;
t3=t1/t2;
t3=5*t2;
t3=6/t2;
}
错误1error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intc:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp14
错误3error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intc:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp15
错误5error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intc:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp18
错误6error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 intc:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp26
错误4error C2804: 二进制“operator /”的参数太多c:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp15
错误2error C2804: 二进制“operator *”的参数太多c:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp14
错误10error C2677: 二进制“/”: 没有找到接受“A”类型的全局运算符(或没有可接受的转换)c:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp39
错误9error C2677: 二进制“*”: 没有找到接受“A”类型的全局运算符(或没有可接受的转换)c:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp38
错误8error C2676: 二进制“/”: “A”不定义该运算符或到预定义运算符可接收的类型的转换c:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp37
错误7error C2676: 二进制“*”: “A”不定义该运算符或到预定义运算符可接收的类型的转换c:\users\hn\documents\visual studio 2008\projects\运算符重载成员函数形式\运算符重载成员函数形式\运算符重载成员函数形式.cpp36
不知道为什么会出错 想用函数的形式实现运算符重载
[解决办法]
楼主,你的运算符重载函数木有写对,已经帮你修改过了。。
/*#include<iostream>using namespace std;class A{public: void sayHi() { cout<<"hello "<<endl; }};int main(void){ A *p = new A(); p->sayHi(); p = NULL; p->sayHi(); system("pause");}*/#include<iostream>using namespace std;class A{private: double x; double y;public: A(double a=0,double b=0) { x=a; y=b; } A operator*(const A &ta); A operator/(const A &ta);};A A::operator *(const A &ta){ A temp; temp.x=this->x*ta.x; temp.y=this->y*ta.y; cout<<temp.x<<endl<<temp.y<<endl; return 0;}A A::operator /(const A &ta){ A temp; temp.x=this->x/ta.x; temp.y=this->y/ta.y; cout<<temp.x<<endl<<temp.y<<endl; return 0;}void main(){ A t1(1,2),t2(3,4),t3; t3=t1*t2; t3=t1/t2; /*t3=5*t2; t3=6/t2;*/}
[解决办法]
另外,楼主的那个重载函数的返回类型也不对的,应该返回类A的对象类型的,
#include<iostream>using namespace std;class A{private: double x; double y;public: A(double a=0,double b=0) { x=a; y=b; } A operator*(const A &ta); A operator/(const A &ta);};A A::operator *(const A &ta){ A temp; temp.x=this->x*ta.x; temp.y=this->y*ta.y; cout<<temp.x<<endl<<temp.y<<endl; return temp;}A A::operator /(const A &ta){ A temp; temp.x=this->x/ta.x; temp.y=this->y/ta.y; cout<<temp.x<<endl<<temp.y<<endl; return temp;}void main(){ A t1(1,2),t2(3,4),t3; t3=t1*t2; t3=t1/t2; /*t3=5*t2; t3=6/t2;*/}