Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Learn how to fix error: invalid use of non-static member function.
Next
Answered
River Wang
  • 29
River Wang
Asked: May 18, 20222022-05-18T17:09:51+00:00 2022-05-18T17:09:51+00:00In: cpp

Learn how to fix error: invalid use of non-static member function.

  • 29

. Advertisement .

..3..

. Advertisement .

..4..

I am working with cpp and getting the error message:

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

Here is the detail of the code that I ran:

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

I need an explanation for the problems I’ve encountered. How to fix invalid use of non-static member function?

invalid use
  • 2 2 Answers
  • 399 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-13T16:24:53+00:00Added an answer on June 13, 2022 at 4:24 pm

    The cause:

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

    Above function is not a non-static member function because none of the member variables of Foo is used in it, so the error: ”invalid use of non-static member function” appears.

    Solution:

    Your error will be solved if Foo::comparator is made static or wrapped in a std::mem_fun object class. lower_bounds() wants the comparer to be a class object which contains a call operator, such as a functor object or function pointer. You can also use a lambda function if C++11 is being used now or in the future. C++11 also possesses std::bind.

    For instance:

    // 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 ...; });
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Zoé Billard
    2022-05-25T20:37:32+00:00Added an answer on May 25, 2022 at 8:37 pm

    It is easiest to make the comparator function static.

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

    Its name will be Foo::comparator when it is invoked in Count.

    It is not a non-static function, so it doesn’t make sense.

    It is possible to make this a non-member operation, especially if you think that Foo might use the comparator.

    • 23
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.