Reputation: 10153
template <int p>
bool FComapare (Node *lId, Node* rId)
{
if(lId->getDiff(p) < rId->getDiff(p))
return true;
else if(lId->getDiff(p) == rId->getDiff(p))
{
if(lId->getLT() < rId->getLT())
return true;
else if(lId->getLT() == rId->getLT())
return (lId->getTFG() < rId->getTFG());
}
return false;
}
vector<set<Node*, bool (*)(Node*,Node*) > > m_F;
for (int i = 0;i < partNum; ++i)
{
//This doesn`t workbecause of const problem...
set<Node *, bool (*)(Node*,Node*) > f(&FComapare<i>);
m_F.push_back(f);
}
I am getting following error
error C2664: 'std::set<_Kty,_Pr>::set(bool (__cdecl *const &)(Node *,Node *))' : cannot convert parameter 1 from 'bool (__cdecl *)(Node *,Node *)' to 'bool (__cdecl *const &)(Node *,Node *)' 1> with 1> [ 1>
_Kty=Node *, 1> _Pr=bool (__cdecl *)(Node *,Node *) 1> ] Reason: cannot convert from 'overloaded-function' to 'bool (__cdecl *const )(Node *,Node *)' 1>
None of the functions with this name in scope match the target type
How can I solve the problem and get the same functionality? How do I define correctly
vector<set<Node*, bool (*)(Node*,Node*) > > m_F;
Thanks
Upvotes: 0
Views: 250
Reputation: 41331
You can't use a variable for the non-type argument.
Instead of a template function, try a function object that stores the value of i
.
class FCompare
{
int p;
public:
FCompare(int p): p(p) {}
bool operator()(const Node *lId, const Node* rId) const {...}
};
set<Node *, FCompare > f((FCompare(i)));
Upvotes: 3
Reputation: 370162
Template arguments need to be known at compile time. You can't use runtime values as template arguments.
Since there is no reason that p
needs to be a template parameter in this case, this isn't a problem though. You can just put the logic of FCompare
into the operator()
of a class, which has p
as a member variable and then pass an instance of that class as the argument to set
's constructor.
Upvotes: 1
Reputation: 599
Template parameters should be known at compile time. In your case you are trying to using a local variable to instantiate a template function that is wrong.
I suppose the best solution is don't use a template, just create a class with operator() that will do the same as FComapare() and store the p as a class member.
Upvotes: 2