作者Tonix517 (大屁孩~)
看板C_and_CPP
标题Re: [问题] template+operator overloading的问题
时间Thu Mar 19 23:06:30 2009
test<float> 和 test<A>实际上是两个不同的类(实际上你可以把template看做带型态的
宏)。而temp1+temp2调用的是 test<float>::operator+(test<float>&) - 如你的模板
定义。
如果要实现temp1+temp2,你必须要特化template<float>然後在里面加一个
template <float>
test::operator+(test<A>&)
※ 引述《x3795566 (冀忆)》之铭言:
: 假设我有一个结构为A 一个class为test
: 然後在main中 我宣告了两个物件temp1,temp2
: test<float> temp1;
: test<A> temp2;
: 然後我欲使temp1+temp2 所以打算多载运算子
: 而有下面程式码(只贴定义部份)
: template <class T>
: float test<T>::operator+(test& i)
: {
: .
: .
: .
: }
: 编译结果显示错误
: no match for 'operator+' in 'temp1 + temp2'
: candidates are: float test<T>::operator+(test<T>&) [with T = float]
: 我的编译器为Dev-C++
: 我有试着将test<A> temp2 改为 test<float> temp2
: 编译就成功了 所以我想问题在於operator+(test& i)的传入参数型态
: 不知道高手可以告知我那边的型态要如何写呢? 感谢<(_ _)>
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.82.82.186
1F:推 x3795566:可是这样会读不到A结构说 因为我的class是写在另外的档案 03/20 01:23
2F:→ avhacker:这篇怎麽全是大圈仔的用语啊 03/20 11:45
3F:→ saxontai:可能原 PO 来自对岸吧 XD 03/20 12:06