运算符号重载问题

zhao_ivan 2003-03-12 05:27:29
class S
{
public:
S(const int in1):a(in1){}
S operator+ (S& other);
friend const S operator* (S& one , S& theother); //ok
// S operator* (S& one, S& theother); //fail
void display();
private:
int a;
};

S S::operator+(S& other)
{
return (a+other.a);
}

const S operator* (S& one, S& theother) //ok
{
return (one.a * theother.a);
}

//S S::operator* (S& one, S& theother)//fail
//{
// return (one.a * theother.a);
//}

void S::display()
{
cout<<"result is "<< a << endl;
}

void main()
{
S n(5),m(6),o(0);
o = n + m;
o.display();
o = n * m;
o.display();
}
...全文
96 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wizlian 2003-03-20
  • 打赏
  • 举报
回复
你还是把他写成friend函数吧~~~~

zhao_ivan 2003-03-14
  • 打赏
  • 举报
回复
DanielWYO(拼命) 说明白了。
DanielWYO 2003-03-13
  • 打赏
  • 举报
回复
如果在类以外,你的operator完全正确.
但是在类以内,本来二元,加上类的一元,变成三元的了.
wizlian 2003-03-13
  • 打赏
  • 举报
回复
o = n * m
相当于
o=o.operator*(n,m)
cenphoenix 2003-03-13
  • 打赏
  • 举报
回复
我也想知道,帮你UP一下!
wizlian 2003-03-13
  • 打赏
  • 举报
回复
重载后是3元啊~~~~~~
ywchen2000 2003-03-13
  • 打赏
  • 举报
回复
int S::operator* (S& one, S& theother)//fail
//{
// return (one.a * theother.a);
//}
or
S S::operator* (S& one, S& theother)//fail
//{
S temp;
// temp.a=one.a * theother.a;
return temp;
//}

zhao_ivan 2003-03-13
  • 打赏
  • 举报
回复
感谢各位回答。
我按照上面的办法(bugfree(八个飞飞))改了还是不行。
编译出错为:binary 'operator *' has too many parameters。
我不理解,‘*’本来就是双目运算符为什么有两个变量不行呢?

还有在运算符号重载时需要注意什么?为什么有的必须是成员变量,有的必须是非静态变量?
bugfree 2003-03-12
  • 打赏
  • 举报
回复
There is some bad programming habit in your code, so you couse some erro, althrough some of
your code without error, but the programming style is not good, you can not debug your program when to writing a little bigger, I rewrite your code, It is not best, I just want to show you something about programming style:
:-)


=======
#include <iostream>
using namespace std;

class S
{
public:
S(const int in1):a(in1){}
S(const S & other):a(other.a){}

S operator+ (const S& one) { return S(a + one.a);};
S operator* (const S& one) { return S((this->a) * one.a);};
S& operator= (const S& one ){ this->a = one.a; return *this; };
void display();

private:
int a;
};

///////////

void S::display()
{
cout<<"result is "<< a << endl;
}

int main()
{
S n(5),m(6),o(0);
o = n + m;
o.display();
o = n * m;
o.display();
return 0;
}
bugfree 2003-03-12
  • 打赏
  • 举报
回复
// S operator* (S& one, S& theother); //fail

this operator return a obj of class S, but you returned a int, one.a*theother.a
so change
return (one.a * theother.a);
to
return S(one.a * theother.a);


/S S::operator* (S& one, S& theother)//fail
//{
// return (one.a * theother.a);
//}

vsfan 2003-03-12
  • 打赏
  • 举报
回复
int S::operator* ( S& theother)
{
return (this->a * theother.a);
}
cenlmmx 2003-03-12
  • 打赏
  • 举报
回复
S S::operator* ( S& theother)
{
return (this->a * theother.a);
}
zhao_ivan 2003-03-12
  • 打赏
  • 举报
回复
为什么,注释掉的地方编译无法通过?

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧