locked
Error C2064: Using Sortfunction with comparison criteria using a member variable of a Class RRS feed

  • Question

  • It turns out that I have the well-known error "C2064 term does not evaluate to a function taking 2 arguments". I have looked at various posts and they recommend the following: Declare the comparison function as 'static', 'virtual', make a regular function and not a method, but those options seem logical but they have not worked for me. I explain my code and a possible reason for my error:

    Code:

    //Grasp.h
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <sstream>
    #include <fstream>
    #include <random>
    #include <ctime>
    using namespace std;
    
    struct NODE {
    	int NodeNumber;
    	string CityName;
    	int Demand;
    	int ReadyTime;
    	int DueTime;
    	int ServiceTime;
    };
    
    class Grasp {
    	
    	public:
    		Grasp ();
    		~Grasp ();		
    		bool clientes_sorter_distancia(const NODE&, const  NODE&);		
                    void CONSTRUCTIVE();  
    
    	private:
    		vector<NODE > Cities;
                    vector<vector<int> > Distance;
    		NODE Depot;
    		NODE actual;
    };
    //Grasp.cpp
    #include "Grasp.h"
    Grasp::Grasp()
    {}
    Grasp::~Grasp()
    {}
    
    bool Grasp::clientes_sorter_distancia(const NODE& lhs, const NODE& rhs) 
    {
    	vector<int> dist = Distance[actual.NodeNumber];	
    	return dist[lhs.NodeNumber] < dist[rhs.NodeNumber];
    }
    
    
    void Grasp::CONSTRUCTIVE()
    {
    //...
    actual = subtour.back();
    sort(cities.begin(), cities.end(), &Grasp::clientes_sorter_distancia); //ordenar clientes por mas cercano a un nodo dado
    //...
    }

    Possible reason:

    In the implementation of the function (clients_sorter_distance) I make use of the "Distance" vector because I need it, but when trying the friend alternative, the error C2064 seems to disappear, but the method tells me that since it no longer belongs to the 'Grasp' class and therefore cannot see the vector.

    How can I fix error C2064 and be able to use the comparison function in the way that I have it implemented?

    Regards.

    Friday, May 1, 2020 8:21 PM

Answers

  • You are looking for something like this:

    std::sort(cities.begin(), cities.end(),
      [this](const NODE& lhs, const NODE& rhs) {
        return clientes_sorter_distancia(lhs, rhs);
      });
    


    Igor Tandetnik

    • Marked as answer by affernan Saturday, May 2, 2020 4:46 PM
    Saturday, May 2, 2020 4:32 AM

All replies

  • You are looking for something like this:

    std::sort(cities.begin(), cities.end(),
      [this](const NODE& lhs, const NODE& rhs) {
        return clientes_sorter_distancia(lhs, rhs);
      });
    


    Igor Tandetnik

    • Marked as answer by affernan Saturday, May 2, 2020 4:46 PM
    Saturday, May 2, 2020 4:32 AM
  • Dear @Igor Tandetnik, your answer has worked. Could you explain to me why the error is in my code? and why is it fixed using your answer? Does it have to do with whether my comparison method is of a class or not?

    Greetings and thanks!

    Saturday, May 2, 2020 1:55 PM