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?

Thanks

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by vijayan121
0

Is this a "specialization" of an existing template for an int dataType? What compiler and OS are you using?

Generally, when you are doing template programming, you want to keep your code as generic as possible.

template <typename T> T doubleValue (T inValue) {
  return (inValue * 2.0);
}

This example function is capable of returning the double of any value supplied. Notice how I have used "T" as a placeholder for the actual dataType whenever a dataType is required.f

This function will work with short int, int, long, float, double, or long double. It's debatable how well it will work with a char because of how they behave.

1

//This doesn`t workbecause of const problem...

template <int p>    
bool FComapare (Node *lId, Node* rId) ;

Here, p must be a constant known at compile-time.

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);
}

Because i is not a constant.

Votes + Comments
Nice explanation
0

How can I solve the problem and get the same functionality?

By using a function object instead of a function. (Sorry, missed this question earlier.)

struct FComapare
{
    explicit FComapare( int i ) : p(i) {}

    bool operator() ( Node* lId, Node* rId ) const
    {
        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;
    }

    int p ;
};

std::vector< std::set< Node*, FComapare > > m_F;

And then,

for( int i = 0 ; i < partNum ; ++i )
    m_F.push_back( std::set< Node*, FComapare >( FComapare(i) ) ) ;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.