关于运算符重载

同桌老王 2004-10-25 04:42:10
编译环境WinXP + VC6


class CInt
{
public:
void setValue(unsigned int v);
unsigned int getValue() const;
CInt();
CInt(unsigned int v);
virtual ~CInt();
void operator=(unsigned int v);
CInt& operator=(CInt&v);
CInt& operator*(const CInt& v1,const CInt& v2);
CInt& operator+(CInt&v1,CInt&v2);
CInt& operator-(CInt&v1,CInt&v2);
CInt& operator*(CInt&v1, unsigned int v2);
CInt& operator+(CInt&v1, unsigned int v2);
CInt& operator-(CInt&v1, unsigned int v2);
CInt& operator*=(CInt&v1);
CInt& operator+=(CInt&v1);
CInt& operator-=(CInt&v1);

private:
unsigned int m_value; // indicate the value
};
错误:
Compiling...
Int.cpp
\int.h(22) : error C2804: binary 'operator *' has too many parameters
\int.h(23) : error C2804: binary 'operator +' has too many parameters
\int.h(24) : error C2804: binary 'operator -' has too many parameters
\int.h(25) : error C2804: binary 'operator *' has too many parameters
\int.h(26) : error C2804: binary 'operator +' has too many parameters
\int.h(27) : error C2804: binary 'operator -' has too many parameters
Error executing cl.exe.
...全文
291 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ra3 2004-10-25
  • 打赏
  • 举报
回复
>CInt& operator+(CInt&v1,CInt&v2);
这里不对,给你个例子

#include <iostream>
using namespace std;

class A
{
int m_num;

public:
A(int num = 0) : m_num(num)
{
}

int Get(void)
{
return m_num;
}

A operator + (A x)
{
return m_num + x.m_num;
}
};

void main()
{
A a(10);
A b(50);
A c;

c = a + b;

cout << c.Get() << endl;
}
ewayne 2004-10-25
  • 打赏
  • 举报
回复
看下面几个例子:

例子1:加号重载

#include<iostream.h>

class Point
{
private:
int x, y;
public:
Point() {}
Point(int i, int j) { x=i; y=j; }
void disp()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
Point operator +(Point &p)
{
return Point(x+p.x, y+p.y);
}
};

void main()
{
Point p1(3, 4), p2(2, 9), p3;
p3=p1+p2;
p3.disp();
}

例子2:等号重载

#include<iostream.h>
class Sample
{
int x;
public:
Sample() {};
Sample(int i) { x=i; }
void disp()
{
cout<<"x="<<x<<endl;
}z
void operator=(Sample &p)
{
x=p.x;
}
};

void main()
{
Sample A(20), B;
Sample C(A);
B=A;
B.disp();
C.disp();
}

例子3:关系运算符重载

#include<iostream.h>
class Rect
{
private:
int length, width;
public:
Rect(int l, int w) { length = l; width =w; }
void disp()
{
cout<<"面积:"<<length * width <<endl;
}
friend int operator > (Rect r1, Rect r2)
{
return r1.length * r1.width>r2.length * r2.width ? 1:0;
}
};


void main()
{
Rect r1(5, 5), r2(4, 6);
cout<<"r1";
r1.disp();
cout<<"r2";
r2.disp();
if(r1>r2)
cout<<"r1面积大于r2的面积"<<endl;
else cout<<"r1面积小于或等于r2的面积"<<endl;
}


同桌老王 2004-10-25
  • 打赏
  • 举报
回复
关于C++运算符重载的例子都是friend的,但是好像没有哪里说过,对于2元运算不能是类成员。各位说是不是?
goodluckyxl 2004-10-25
  • 打赏
  • 举报
回复
用friend实现

单目可以不用
carylin 2004-10-25
  • 打赏
  • 举报
回复
楼主可以改成这样:
friend CInt& operator*(const CInt& v1,const CInt& v2);
friend CInt& operator+(CInt&v1,CInt&v2);
friend CInt& operator-(CInt&v1,CInt&v2);
friend CInt& operator*(CInt&v1, unsigned int v2);
friend CInt& operator+(CInt&v1, unsigned int v2);
friend CInt& operator-(CInt&v1, unsigned int v2);
carylin 2004-10-25
  • 打赏
  • 举报
回复
操作符重载时若是类的成员函数,其行参只能是一个。

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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