C ++ error: 'bool operator () (std :: pairNode &, float, std :: pairNode &, float)' must be a nonstatic member function

0

I'm using a priority queue consisting of a pair of an object of a class called Nodo and a value of type float (I chose it instead of double, thinking it takes up less bytes).

The error I get is in the comparison operator > that I overloaded because the priority queue that used the values located higher in the queue are the smallest, however, I am a beginner in C ++, and despite to check the code again and again, I have not been able to debug the errors. Codes (declaration and definition, in class Nodo ):

 friend bool operator()(const pair<Nodo&,float> nodo1,const pair<Nodo&,float> nodo2);

 bool car::operator()(const pair<Nodo& a,float cost1> nodo1,const pair<Nodo& b,float cost2> nodo2){

        return (nodo1.second) > (nodo2.second)

 }

errors:

error: ‘bool operator()(std::pair<Nodo&, float>, std::pair<Nodo&, float>)’ must be a nonstatic member function
         friend bool operator()(const pair<Nodo&,float> nodo1,const pair<Nodo&,float> nodo2);
                                                                                           ^
car.cpp:68:56: error: wrong number of template arguments (1, should be 2)
     bool car::operator()(const pair<Nodo& a,float cost1> nodo1,const pair<Nodo& b,float cost2> nodo2){

I would also appreciate if you could give me some practical tips on debugging the code. Thanks

    
asked by AER 09.11.2018 в 16:45
source

1 answer

2

It's easy. A function friend can not be a member function ... that's why you declare it friend , so you can access the private variables of the class without being a member of it:

struct Test1
{
  Test1 operator+(Test1 const&) const
  {
      std:: cout << "Funcion miembro\n";
      return *this;
  }
};

struct Test2
{
  friend Test2 operator+(Test2 const&, Test2 const&);
};

Test2 operator+(Test2 const&, Test2 const&)
{
    std:: cout << "Funcion friend\n";
    return Test2();
}

int main()
{
    Test1 t1;
    t1+t1;

    Test2 t2;
    t2 + t2;;
}

I have not set an example with the operator function because this operator only can be a member function:

class car
{
public:

    bool operator(pair<Nodo&,float> const& nodo1,pair<Nodo&,float> const& nodo2);
};

bool car::operator()(pair<Nodo&,float> const& nodo1,pair<Nodo&,float> const& nodo2)
{
  // ...
}
    
answered by 10.11.2018 / 04:48
source