2010-11-20 118 views
1

我必須聲明爲這樣的模板類的成員函數:C++模板沒有匹配的函數調用

template <class T> 
int Data<T>::getPosition(vector<T> stuff, T newStuff, bool ascending) 

我把這個地方與線

frequencies.insert(frequencies.begin() + getPosition(frequencies, current, ascending), 
         frequencies[i]); 

該行的變量聲明爲:

vector<T> temp; 
vector<int> frequencies; 
int current = frequency.find(words[i])->second; 

但是,致電getPosition的電話給出了此錯誤:

Data.h|158|error: no matching function for call to 'primitives::Data<double>::getPosition(std::vector<int, std::allocator<int> >&, int&, bool&)'| 
Data.h|165|note: candidates are: int primitives::Data<T>::getPosition(std::vector<T, std::allocator<_CharT> >, T, bool) [with T = double]| 

我在做什麼錯在這裏?

+0

您需要提供更多的上下文,因爲不清楚如何選擇模板參數T.你是從primitives :: Data的另一個成員函數調用它嗎? – Stewart 2010-11-20 20:13:51

+2

另外,你可能不想通過值傳遞一個向量 - 這將複製向量,但你可能會發現你可以通過const ref傳遞它,這是更有效得多 – Stewart 2010-11-20 20:14:48

+0

@Stewart yes all that代碼位於基元:: Data類的內部。另外,謝謝你的建議;我將實現const引用 – wrongusername 2010-11-20 20:30:45

回答

1

你的函數原型被模板上Data<t>,它看起來像你的對象上執行這個調用與Data<double>類型和傳遞std::vector<int>int,當它可能需要一個std::vector<double>double對應於初始模板類型的Data對象。

+0

你發佈它就像我提交一個答案我的問題幾秒鐘:) – wrongusername 2010-11-20 20:37:04

0
vector<T> temp; 

不應該在這裏像int,double或bool類型的?

+0

Nah,它應該存儲'Data'類的任何類型的東西。 – wrongusername 2010-11-20 20:31:34

+0

好吧,看起來你正在使用一個雙數據類,它期望矢量東西(getPosition的第一個參數)也是雙倍的。但是,您將第一個位置傳遞給矢量。 – Naveen 2010-11-20 20:38:32

2

getPosition需要三個參數vector<T>,Tbool。在這種情況下,模板類型Tdouble(如錯誤消息中所示),但您試圖分別將vector<int>int分別作爲第一個參數和第二個參數。

或許getPosition的參數不應該模板化?取決於你想要達到的目標 - 畢竟,你確實有硬編碼的int向量。