0
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

1
  • 2
    "How can I solve the problem and get the same functionality?" - What functionality? You forgot to tell us exactly what functionality you want in the first place. What end goal are you trying to achieve here? Commented Dec 19, 2010 at 20:03

3 Answers 3

3

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)));
Sign up to request clarification or add additional context in comments.

2 Comments

set<Node *, FCompare > it doesn`t compile
vector<set<Node*, bool ()(Node,Node*) > > m_F; so how can I define set correctly?
2

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.

Comments

1

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.

1 Comment

How can I define m_F in this case?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.