C++ 非静态成员函数的无效使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29286863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:10:19  来源:igfitidea点击:

invalid use of non-static member function

c++functionconstmembernon-static

提问by Nash

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::Countshould 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:

该方法Foo::Count应使用std::lower_bound()在找到元件vector<Bar>根据对的两个串。现在是不起作用的部分。要lower_bound()我提供方法comparator()。我认为没关系,但是 g++ 说:

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...

方法Count()必须保持const......

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

我对 C++ 很陌生,因为我被迫学习它。

Any ideas?

有任何想法吗?

采纳答案by Matthew

You must make Foo::comparatorstatic or wrap it in a std::mem_funclass 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 dwcanillassuggests and use a lambda function. C++11 also has std::bindtoo.

您必须将其Foo::comparator设为静态或将其包装在std::mem_fun类对象中。这是因为lower_bounds()期望比较器是具有调用运算符的对象类,如函数指针或函子对象。此外,如果您使用的是 C++11 或更高版本,您也可以按照dwcanillas 的建议使用 lambda 函数。C++11 也有std::bind

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

回答by M.M

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.

在 中调用它时Count,它的名称将是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.

你现在拥有它的方式,成为一个非静态成员函数是没有意义的,因为它不使用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.

另一种选择是使它成为一个非成员函数,特别是如果这个比较器可能被除了Foo.

回答by edmz

You shall pass a thispointer to tell the function which object to work on because it relies on that as opposed to a staticmember function.

您应该传递一个this指针来告诉函数要处理哪个对象,因为它依赖于该对象而不是static成员函数。