• Register
100 points
7 2
Welcome to Kodlogs, programming questions and answer website.

Static member means inside a function is that once the variable has been initialized, it will remain the same in the memory until the end of the program. The error “invalid use of non-static member function” means that we are using a non-static member function in a way which is not possible.

Cause

This error occurs because the function that requires that member of the class we are using, expects it to be of a class of object that has a call operator.
 

Solution

The easiest method to fix this problem is to make the function be static.
 
You better understand it via example. 
 

Example

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

Here lower_bounds() expects the comparer to be a class of object that has a call operator.
The way the programmer has the comparator function does not make any sense to be a non-static member function since it does not use any member variables of Foo.

Hope this will help you.
 

100 points
7 2