作者wintercobra (wintercobra)
看板EE_DSnP
标题[问题] hw1.2.p3c的template
时间Fri Oct 15 16:43:51 2010
(这是一个使用template class,在呼叫时compile不过的问题)
作业上说selectionSort的prototype是这样
template <class T>
void selectionSort(vector<T>& array, const Compare<T>& compare)
我在main()里面要呼叫的时候,写
selectionSort( <int>data, Greater <int> () ); <----compile不过
^^^^data是我用来存字串array的vector
我的Greater是这样定义的
template <class T>
class Greater: public Compare<T>
{
...
}
结果compile不过,讯息是 ( 错在main()中呼叫selectionSort()的那一行 )
error: expected primary-expression before '<' token
error: expected primary-expression before 'int'
请问:是我呼叫"functional object兼template"的方法错误吗?
我只知道函数的template呼叫时是: f<int>(a, b);
而class的template要instantiate一个物件的时候是用: myClass <int> ();
但是同时要用functional object(没有object)配上template,该怎麽做呢?
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.218.186
※ 编辑: wintercobra 来自: 140.112.218.186 (10/15 16:45)
1F:推 tomap41017:你selectionSort的第一个参数不觉的怪怪的吗@@? 10/15 20:58
2F:→ tomap41017:应该是selectionSort(data, Greater<int>()); 10/15 20:58
3F:→ tomap41017:template的宣告应该是ok... 10/15 20:59
4F:→ tomap41017:你的编译器告诉你的错误讯息就是指那个地方有错罗 10/15 21:00
5F:推 Astone:问题+1 complie在呼叫函式时一直过不了= = 10/15 22:16
6F:推 cktigeryang:他会自己配对,你就把它想成是overload一个函式就好了 10/15 22:31
7F:推 ric2k1:推 selectionSort(data, Greater<int>()); 10/15 23:02
8F:→ wintercobra:原来他自己会判定data啊...谢谢~ 10/15 23:35
10F:推 tomap41017:楼上的感谢你XD 10/17 23:12