【问题标题】:Template Issues - Having trouble passing function as args (invalid use of non-static member function)模板问题 - 将函数作为 args 传递时遇到问题(无效使用非静态成员函数)
【发布时间】:2010-11-06 16:45:27
【问题描述】:

这是我制作的模板。我相信这是不言自明的

   template <class N, class I>
    std::list<N*> selectiveList(I element, std::list<N*> & container, I (N::*f)() const) {
 typename std::list<N*>::iterator it;

 std::list<N*> selectiveListing;

 for (it = container.begin(); it != container.end(); ++it)
  if ((*it)->*f == element)
   selectiveListing.push_back(*it);

 if (selectiveListing.size() == 0)
  throw NoItemsFound<I>(element);

 return selectiveListing;
}

我从这个函数中调用它:

void AirportManager::searchAirplanesTypes() {
 clrscr();
 std::stringstream prompt;
 prompt  <<  "Escolha atributo a pesquisar: \n" <<
    "1) Tipo\n" <<
    "2) Descricao\n" << "3) Categoria\n";

 int choice = getNumberInput(prompt.str(), 1, 3);

 switch (choice) {
 case 1: {
  std::string searchElement1 = getStringInput("Tipo: ");
  pageNav(selectiveList(searchElement1, airport.airplanesTypes,
    &AirplaneType::getType), "", true, true);
  break;
 }
 case 2: {
  std::string searchElement2 = getStringInput("Descricao: ");
  pageNav(selectiveList(searchElement2, airport.airplanesTypes,
    &AirplaneType::getDescription), "", true, true);
  break;
 }
 case 3: {
            //Category is an enumerated data type
  Category searchElement3 = getCategoryInput("Categoria: ");
  pageNav(selectiveList(searchElement3, airport.airplanesTypes,
    &AirplaneType::getCategory), "", true, true);
  break;

 }
 }
}

我真的看不出有什么问题。然而,当我编译它时,会发生以下情况:

In file included from ..\src\AirportManager.cpp:14:0:
..\src\/headers/Template.h: In function 'std::list<N*> selectiveList(I, std::list<N*>&, I (N::*)()const) [with N = AirplaneType, I = std::basic_string<char>]':
..\src\AirportManager.cpp:1259:34:   instantiated from here
..\src\/headers/Template.h:340:3: error: invalid use of non-static member function
..\src\/headers/Template.h: In function 'std::list<N*> selectiveList(I, std::list<N*>&, I (N::*)()const) [with N = AirplaneType, I = Category]':
..\src\AirportManager.cpp:1265:31:   instantiated from here
..\src\/headers/Template.h:340:3: error: invalid use of non-static member function

这些有点随机。我看不出静力学是如何在这里发挥作用的。所有引用的 getter 都是简单的 const 返回。

我很乐意得到一些意见。

【问题讨论】:

    标签: c++ templates static compiler-errors


    【解决方案1】:

    if (((*it)-&gt;*f)() == element)

    【讨论】:

      【解决方案2】:

      复制元素的规范方法是使用否定的std::remove_copy_if() 或编写copy_if() 算法。恕我直言copy_if() 是更清晰的选择,尽管它不是标准函数,但它很容易正确编写:

      template<class In, class Out, class Pred>
      Out std::copy_if(In first, In last, Out res, Pred p)
      {
          while( first != last ) {
              if( p(*first) ) *res++ = *first;
              ++first;
          }
          return res;
      }
      

      然后你可以像这样使用它:

      if( choice == 1 ) {
          copy_if(airport.airplanesTypes.begin(), airport.airplanesTypes.end(),
                  std::back_inserter(result),
                  boost::bind(&AirplaneType::getType, _1, searchElement1));
          pageNav(result, "", true, true);
      } else if( choice == 2 ) {  // ...
      

      这里的好处是您可以将copy_if() 用于许多其他事情。它适用于任何容器,而不仅仅是std::list&lt;N*&gt;,它适用于任何一元谓词,无论是原始函数还是函数对象。

      【讨论】:

      • 感谢您的补充!我将考虑到最终的下一个项目,但现在重构所有功能的成本太高。
      【解决方案3】:

      已经有一段时间了,但是你错过了一个新的吗?

      throw new NoItemsFound<I>(element);
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-18
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多