operator = + 相关问题 。。。。

linoom 2010-05-13 10:50:39
能给我描述下下面代码 operator 错误的原因??
例如 Test t1(1, 2), t2(2, 1) t;
t = t1 + t2;
怎么出现重载错误 错误: no match 为‘operator=’在‘t = t1.Test<T1, T2>::operator+ [with T1 = int, T2 = int](((Test<int, int>&)(& t2)))’中
a.cc:31: 附注: 备选为: Test<T1, T2>& Test<T1, T2>::operator=(Test<T1, T2>&) [with T1 = int, T2 = int]

编译器 gcc 4.3.3

程序如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>

using namespace std;

template <class T1, class T2>
class Test {
public:
Test(T1 xx = T1(0), T2 yy = T2(0));
Test<T1, T2>& operator = (Test<T1, T2>& t);
Test<T1, T2> operator + (Test<T1, T2>& t);
Test<T1, T2> operator - (Test<T1, T2>& t);
T1 get_x() {return x; }
T2 get_y() {return y; }
private:
T1 x;
T2 y;
};

template <class T1, class T2>
Test<T1, T2>::Test(T1 xx, T2 yy)
: x(xx)
, y(yy)
{
}

template <class T1, class T2>
Test<T1, T2>& Test<T1, T2>::operator = (Test<T1, T2>& t)
{
x = t.x;
y = t.y;
}

template <class T1, class T2>
Test<T1, T2> Test<T1, T2>::operator + (Test<T1, T2>& t)
{
Test<T1, T2> tmp;

tmp.x = x + t.x;
tmp.y = y + t.y;

return tmp;
}

template <class T1, class T2>
Test<T1, T2> Test<T1, T2>::operator - (Test<T1, T2>& t)
{
Test<T1, T2> tmp;

tmp.x = x - t.x;
tmp.y = y - t.y;

return tmp;
}

class Bridge : public ostream {
public:
enum Type {op1, op2};
Bridge(ostream& _os, Type _type) : os(_os), type(_type) {}
template <class T1, class T2>
ostream& operator<< (Test<T1, T2>& t);
private:
ostream& os;
Type type;
};

template <class T1, class T2>
ostream& operator<< (ostream& os, Test<T1, T2>& t)
{
Bridge* pB = 0;
if ((pB = dynamic_cast<Bridge*>(&os)) != 0) {
*pB <<t;

delete pB;
return os;
}

os <<"t.x == " <<t.get_x() <<endl;
os <<"t.y == " <<t.get_y() <<endl;

return os;
}

template <class T1, class T2>
ostream& Bridge::operator<< (Test<T1, T2>& t)
{
if (type == Bridge::op1) {
os <<"op1.. x == " <<t.get_x() <<endl;
} else if (type == Bridge::op2) {
os <<"op2.. y == " <<t.get_y() <<endl;
}

return os;
}

ostream& op1(ostream& os)
{
Bridge* pB = new Bridge(os, Bridge::op1);

return *pB;
}

ostream& op2(ostream& os)
{
Bridge* pB = new Bridge(os, Bridge::op2);

return *pB;
}

int main()
{
Test<int, int> t1(11, 22);
Test<int, int> t2(11, 22);
Test<int, int> t(0, 0);

cout <<t1;
cout <<t2;

t = t1 + t2;

cout <<op1 <<t;
cout <<op2 <<t;
}

...全文
82 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
aweer 2010-05-13
  • 打赏
  • 举报
回复
直接注释掉 operator= 重载操作符可以按你效果运行。。

主要问题是operator= 被认为是候选函数了

但你的程序并不是用的operator=
linoom 2010-05-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 arong1234 的回复:]

记住:所有参数都应该是常引用,而不是引用
例如
Test<T1, T2>& Test<T1, T2>::operator = (Test<T1, T2>& t)
需要是
Test<T1, T2>& Test<T1, T2>::operator = (const Test<T1, T2>& t)

a+b返回的临时值虽然名义上可以修改,但是修改一个临时的……
[/Quote]
哈哈 按照您的提示调试好了
多谢了。。。
cattycat 2010-05-13
  • 打赏
  • 举报
回复
看来是我的vc6不行?你确定只有一个错误是oprator那吗,按2楼的改吧,加上const修饰试试
cattycat 2010-05-13
  • 打赏
  • 举报
回复
看你operator+没有问题啊,我试了下,是你的Brige继承了ostream,后者没有默认的构造函数,所以继承的不对。
arong1234 2010-05-13
  • 打赏
  • 举报
回复
记住:所有参数都应该是常引用,而不是引用
例如
Test<T1, T2>& Test<T1, T2>::operator = (Test<T1, T2>& t)
需要是
Test<T1, T2>& Test<T1, T2>::operator = (const Test<T1, T2>& t)

a+b返回的临时值虽然名义上可以修改,但是修改一个临时的值毫无意义,所以好的编译器会把这个当作程序设计者的错误!

把所有的参数种的Test<T1,T2>&改为 const Test<T1,T2>&就应该好了
昵称很不好取 2010-05-13
  • 打赏
  • 举报
回复
int main()
{
Test<int, int> t1(11, 22);
Test<int, int> t2(11, 22);
// Test<int, int> t(0, 0);

cout <<t1;
cout <<t2;

Test<int, int> t = t1 + t2;//代码中operator= 忘了没有写返回值

cout <<op1 <<t;
cout <<op2 <<t;
}

64,637

社区成员

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

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