[c++] invalid use of non-static member function

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?

This question is related to c++ function constants member non-static

The answer is


You shall pass a this pointer to tell the function which object to work on because it relies on that as opposed to a static member function.


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.


Examples related to c++

Method Call Chaining; returning a pointer vs a reference? How can I tell if an algorithm is efficient? Difference between opening a file in binary vs text How can compare-and-swap be used for a wait-free mutual exclusion for any shared data structure? Install Qt on Ubuntu #include errors detected in vscode Cannot open include file: 'stdio.h' - Visual Studio Community 2017 - C++ Error How to fix the error "Windows SDK version 8.1" was not found? Visual Studio 2017 errors on standard headers How do I check if a Key is pressed on C++

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to constants

Constants in Kotlin -- what's a recommended way to create them? Why Is `Export Default Const` invalid? Proper use of const for defining functions in JavaScript Declaring static constants in ES6 classes? How can I get the size of an std::vector as an int? invalid use of non-static member function Why does JSHint throw a warning if I am using const? Differences Between vbLf, vbCrLf & vbCr Constants Constant pointer vs Pointer to constant Const in JavaScript: when to use it and is it necessary?

Examples related to member

invalid use of non-static member function An object reference is required to access a non-static member C++ callback using class member cout is not a member of std php static function Static nested class in Java, why?

Examples related to non-static

invalid use of non-static member function Difference between Static methods and Instance methods C# error: "An object reference is required for the non-static field, method, or property" Non-static variable cannot be referenced from a static context Calling Non-Static Method In Static Method In Java