繁体   English   中英

在C ++中回调成员函数

[英]call back member function in c++

    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()函数,该函数是类“ scanner”的成员函数。如何使该成员函数AddToIndex成为回调函数? 以及如何解决这个问题...

在调用ftw ,第一个参数是mRootFilePath.c_str 也许您想要mRootFilePath.c_str()代替?

除了Managu的答案外,别忘了确保您的static成员函数使用C链接( extern "C" )-根据实现的不同,您可能无法将指针传递给具有默认C ++链接的函数,其中需要一个指向C函数的指针(当ABI不同时)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM