Resolve the Exception “error: invalid use of non-static member function”

C++ is a cross-platform, general-purpose, and high-level programming language designed by Bjarne Stroustrup. Programmers and developers use C++ to build operating systems, games, browsers, and applications. It is also used to perform other tasks like data structure and software engineering tasks. When using C++, you may encounter the exception “error: invalid use of non-static member function”.

How do you get the error warning?

When you are working with a C++ project using the below program script

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

To find the element in vector<Bar>, use std::lower_bound() by Foo::Count method as per pair of two strings. Using the comparator() by lower_bound(), you get the following error:

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

That’s exactly how you land up in the trouble. Let’s figure out the way to get rid of it:

Solutions To Fix the Error Warning “error: invalid use of non-static member function”

Solution 1

In case the function you used is not static, you get the error warning because no member variables of Foo are found in it. For instance, the following cause the error:

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

To fix the error, you just need to make a wrapped or static Foo::comparator in a class object std::mem_fun. The comparer is expected to be an object’s class having a call operator like function object or function pointer by the lower_bounds(). You can use the lambda function in case you are using C++11 or the recent one to create a project. C++11 is also known to have std::bind. Have a look at the example to try out:

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

Solution 2

Another solution to resolve the error by easily make the comparator function static. Follow this:

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

If you invoke it in the count, the Foo::comparator is going to be the name. It is a completely wrong way to have a non-static member function as it is unable to use Foo’s member variables.  An alternative option is to have a non-member function for the comparator being used by any other code rather than just Foo.

Conclusion

In this article, we shed light on the solutions to resolve the error message “error: invalid use of non-static member function”. You can simply use the above solutions to get rid of the error warning.

I hope you find it interesting and informative! I wish you a happy error-solving!

For further assistance, you can drop a message below in the comment box. You can also refer to other posts on the site to get other issues fixed too.

Posted in C++

Leave a Reply

Your email address will not be published. Required fields are marked *