【问题标题】:call back member function in c++c++中回调成员函数
【发布时间】:2009-10-01 20:34:49
【问题描述】:
    class scanner
    {
        private:
            string mRootFilePath;
            static int  AddToIndex( const char *,const struct stat *,int);  
        public:

            scanner(string aRootFilePath){
            mRootFilePath = aRootFilePath;
            }       

            string GetFilepath(){
                return mRootFilePath;
            }
            void Start();
    };


    int  scanner :: AddToIndex(const char *fpath, const struct stat *sb,int typeflag)
    {

        fprintf(stderr,"\n%s\t%d",fpath,typeflag);
        return 0;
    }

    void scanner :: Start()
    {
        ftw(mRootFilePath.c_str,(int (*)( const char *,const struct stat *,int))scanner :: AddToIndex,200);
    }


main()
{

int i;
scanner test(".");
test.Start();


}

当我编译这段代码时,我收到错误消息

 main.c: In member function ‘void scanner::Start()’:
main.c:34: error: argument of type ‘const char* (std::basic_string<char, std::char_traits<char>, std::allocator<char> >::)()const’ does not match ‘const char*’

ftw 函数在这种情况下调用回调函数 AddToIndex() 函数,它是“扫描器”类的成员函数..如何使这个成员函数 AddToIndex 作为回调函数?以及如何解决这个问题...

【问题讨论】:

标签: c++ c linux


【解决方案1】:

在您对ftw 的调用中,第一个参数是mRootFilePath.c_str。也许你想要mRootFilePath.c_str()

【讨论】:

    【解决方案2】:

    除了 Managu 的回答之外,不要忘记确保您的 static 成员函数使用 C 链接 (extern "C") - 根据实现,您可能无法使用需要指向 C 函数的指针的默认 C++ 链接(当 ABI 不同时)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-08
      • 2010-11-03
      • 2013-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多