Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

invalid use of non-static member function [duplicate]

I have something like this:

class Bar       {       public:         pair<string,string> one;         std::vector<string> cars;         Bar(string one, string two, string car);       };  class Car       {       public:         string rz;         Bar* owner;         Car(string car, Bar* p);       };  class Foo        {          public:           Foo  ( void );           ~Foo  ( void );           int Count ( const string & one, const string &  two) const;           int comparator (const Bar & first, const Bar & second) const;                         std::vector<Bar> bars;        };  int Foo::comparator(const Bar & first, const Bar & second) const{   return first.name < second.name; }  int Foo::Count  ( const string & one, const string & two ) const{   int result=0;   Bar mybar = Bar( one, two, "" );   std::vector<Bar>::iterator ToFind = lower_bound(bars.begin(), bars.end(), mybar, comparator);   if (ToFind != bars.end() && ToFind->one == mybar.one ){     result = ...   }   return result; } 

The method Foo::Count should use std::lower_bound() to find element in vector<Bar> according to pair of two strings. Now the part which doesn't work. To lower_bound() I'm providing method comparator(). I thought it was okay, but g++ says:

c.cpp: In member function ‘int Foo::Count(const string&, const string&) const’: c.cpp:42:94: error: invalid use of non-static member function std::vector<Bar>::iterator ToFind = lower_bound(bars.begin(), bars.end(), mybar, comparator); 

And the method Count() must stay const...

I'm quite new to C++ because I'm forced to learn it.

Any ideas?

like image 390
Nash Avatar asked Mar 26 '15 19:03

Nash


People also ask

What is a nonstatic member function?

A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)

How do you declare a static member function in C++?

Static Function Members By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.

What is a static function in C++?

Static Function: It is a member function that is used to access only static data members. It cannot access non-static data members not even call non-static member functions. It can be called even if no objects of the class exist.


2 Answers

The simplest fix is to make the comparator function be static:

static int comparator (const Bar & first, const Bar & second); ^^^^^^ 

When invoking it in Count, its name will be Foo::comparator.

The way you have it now, it does not make sense to be a non-static member function because it does not use any member variables of Foo.

Another option is to make it a non-member function, especially if it makes sense that this comparator might be used by other code besides just Foo.

like image 195
M.M Avatar answered Sep 21 '22 14:09

M.M


You must make Foo::comparator static or wrap it in a std::mem_fun class object. This is because lower_bounds() expects the comparer to be a class of object that has a call operator, like a function pointer or a functor object. Also, if you are using C++11 or later, you can also do as dwcanillas suggests and use a lambda function. C++11 also has std::bind too.

Examples:

// Binding: std::lower_bounds(first, last, value, std::bind(&Foo::comparitor, this, _1, _2)); // Lambda: std::lower_bounds(first, last, value, [](const Bar & first, const Bar & second) { return ...; }); 
like image 21
Matthew Avatar answered Sep 24 '22 14:09

Matthew