如何将类中定义的函数作为参数传递给函数

How to pass functions defined in a class as arguments to functions?

本文关键字:函数 参数传递 定义      更新时间:2023-10-16

getMessage 方法提取输入字符串的每个单词中的第一个字母。

例: input ="查找此句子的第一个字母"

输出 = FtflotS

        #include <iostream>
        #include <string>
        #include <vector>
        #include <algorithm>
        #include <cctype>
        using namespace std;
        class HiddenMessage {
        public:
          bool space (char c) {
            return isspace(c);
          }
          bool not_space (char c) {
            return !isspace (c);
          }
          string getMessage(string text) {
            string ret;
            typedef string::const_iterator iter;
            iter  i, j;
            i = text.begin();
            while (i != text.end()) {
              i = find_if (i, text.end(), not_space); // error here
              j = find_if (i, text.end(), space); // error here
              if (i != text.end()) {
                  ret += *i;
              }
              i = j;
            }
            return ret;
          }
        };
        //compiler error:
        //error: invalid use of non-static member function

我尝试将空间和not_space定义为静态的,它确实如此不工作。

getMessage 从下面的主调用:

    #include <ctime>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <sstream>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    int main(int argc, char* argv[])
    {
      if (argc == 1) 
        {
          cout << "Testing HiddenMessage (250.0 points)" << endl << endl;
          for (int i = 0; i < 20; i++)
            {
              ostringstream s; s << argv[0] << " " << i;
              int exitCode = system(s.str().c_str());
              if (exitCode)
                cout << "#" << i << ": Runtime Error" << endl;
            }
          int T = time(NULL)-1456061889;
          double PT = T/60.0, TT = 75.0;
          cout.setf(ios::fixed,ios::floatfield);
          cout.precision(2);
          cout << endl;
          cout << "Time  : " << T/60 << " minutes " << T%60 << " secs" << endl;
          cout << "Score : " << 250.0*(.3+(.7*TT*TT)/(10.0*PT*PT+TT*TT)) << " points" << endl;
        }
      else
        {
          int _tc; istringstream(argv[1]) >> _tc;
          HiddenMessage _obj;
          string _expected, _received;
          time_t _start = clock();
          switch (_tc)
            {
            case 0:
              {
                string text = "compete online design event rating";
                _expected = "coder";
                _received = _obj.getMessage(text); break;
              }
            case 1:
              {
                string text = "  c    o d     e      r    ";
                _expected = "coder";
                _received = _obj.getMessage(text); break;
              }
            case 2:
              {
                string text = "round  elimination during  onsite  contest";
                _expected = "redoc";
                _received = _obj.getMessage(text); break;
              }
            case 3:
              {
                string text = " ";
                _expected = "";
                _received = _obj.getMessage(text); break;
              }
              /*case 4:
                {
                string text = ;
                _expected = ;
                _received = _obj.getMessage(text); break;
                }*/
              /*case 5:
                {
                string text = ;
                _expected = ;
                _received = _obj.getMessage(text); break;
                }*/
              /*case 6:
                {
                string text = ;
                _expected = ;
                _received = _obj.getMessage(text); break;
                }*/
            default: return 0;
            }
          cout.setf(ios::fixed,ios::floatfield);
          cout.precision(2);
          double _elapsed = (double)(clock()-_start)/CLOCKS_PER_SEC;
          if (_received == _expected)
            cout << "#" << _tc << ": Passed (" << _elapsed << " secs)" << endl;
          else
            {
              cout << "#" << _tc << ": Failed (" << _elapsed << " secs)" << endl;
              cout << "           Expected: " << """ << _expected << """ << endl;
              cout << "           Received: " << """ << _received << """ << endl;
            }
        }
    }

你有两个问题。

首先,你向需要函数对象或指针的find_if提供非静态类成员函数(spacenot_space)。因此,如果您希望它们保留您的类,请static声明它们,或者将它们放在类之外来使它们全局化。

第二,您的字符串text参数是非常量参数,但是,您正在使用const交互器类型。 begin()end()调用将返回 const 或 non-const 迭代器,具体取决于调用对象(在本例中为 text)以及它是否限定 const 。因此,请将text参数声明为 const