Reputation: 1711
I have been hitting my head against a brick wall for a bit and decided to submit and just post my error.
I have a template (shown below) that passes a std::map, which is inside a namespace. This compiles fine.
My problem occurs when trying to call the function template. I get the below error:
error: no matching function for call to
'getValuePointer(Muon*&, std::map<int, MET*, std::less<int>,
std::allocator<std::pair<const int, MET*> > >*&)'
What am I doing wrong?
Here is the template code:
#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
Here is an example call to the template function
C = ObjectMapMatch::getValuePointer<ClassC*, int, ClassD*>(A, B);
Where:
C is ClassC*
A is ClassC*
B is std::map<int,ClassD*>*
What am I doing wrong?
Upvotes: 0
Views: 937
Reputation: 76246
In your last edit you say that
B is std::map<int,ClassD*>*
but your function is
template< class A, class B, class C >
C getValuePointer( A &x , map< B,C> &y )
thus accepting
std::map<int,ClassD*> &
for the second argument. So pointer to map vs. reference to map. I suppose you should either be passing *B
, or B
should not be pointer (STL is there so you don't need to work with pointers much so try avoiding them as much as possible).
Upvotes: 0
Reputation: 110069
According to the error message, you passed a pointer to a map as the second parameter, while the function expects a reference to a map.
You can see this because of the *
at the end here:
no matching function for call to
'getValuePointer(Muon*&, std::map<...>*&)'
^
There will be additional issues once you fix this though: Since x
is of type ClassC*
, calling x.Index()
will give an error. Maybe the first template parameter should be ClassC
instead of ClassC*
.
Upvotes: 3
Reputation: 10940
your signature is wrong
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);
}
}
and the call it
ObjectMapMatch::getValuePointer<ClassC, int, ClassD>(A, B);
but C is ClassD*, not ClassC* right?
Upvotes: 0
Reputation: 12202
Your template definition says:
C getValuePointer( A &x , map< B,C> &y )
And you are calling this function with:
ObjectMapMatch::getValuePointer<ClassC*, int, ClassD*>(A, B);
... and you say that B is:
std::map<int,ClassD*>
So, for your template B is an int, and C is a ClassD*... That's why there is not a match.
Or maybe not, I mean, the name you chose for the values is the same for the classes in your template, which is quite confusing.
Hope this helps.
Upvotes: 0