我一直在磚牆上碰撞一下頭,並決定提交併發佈我的錯誤。將映射傳遞給C++中的模板函數時出錯
我有一個模板(如下所示),它傳遞一個名稱空間內的std :: map。這編譯好。
嘗試調用函數模板時出現我的問題。我得到下面的錯誤:
error: no matching function for call to
'getValuePointer(Muon*&, std::map<int, MET*, std::less<int>,
std::allocator<std::pair<const int, MET*> > >*&)'
我在做什麼錯?
這裏是模板代碼:
#ifndef OBJECTMAPMATCH_H
#define OBJECTMAPMATCH_H
#include <map>
#include <utility>
#include <typeinfo>
#include <iostream>
#include <stdlib.h>
using namespace std;
namespace ObjectMapMatch {
template< class A, class B, class C >
C getValuePointer(A &x , map< B,C> &y)
{
if(y.find(x.Index()) != y.end()){
return y.find(x.Index()).second;
}else{
cout << "ERROR:ObjectMapMatch::getValuePointer:Can not Find "
<< typeid(y).name() << " FOR " << typeid(x).name() << endl;
exit(1);
}
}
}
#endif
下面是一個例子調用模板函數
C = ObjectMapMatch::getValuePointer<ClassC*, int, ClassD*>(A, B);
其中:
C is ClassC*
A is ClassC*
B is std::map<int,ClassD*>*
我在做什麼錯?
我構建了一個[類似的例子](http://ideone.com/Relqo),它運行良好。 –
這不是一個解決方案,但只是一個有用的註釋 - 除非我錯了,在這種情況下可以使用所謂的「模板類型推斷」,並簡化對'C = ObjectMapMatch :: getValuePointer(A,B) 。類型''將被自動推斷 –
Nate
我將添加到Nate,您不僅可以,而且應該*應該使用模板類型推理(當然只適用於函數模板)。 –