call back member function in c++

2019-09-06 09:15发布

    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();


}

When I compile this code I get error message

 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 function calls the call back function in this case AddToIndex() function which is a member function of the class "scanner".. how can make this member function AddToIndex as a call back function? And How to resolve this issue...

标签: c++ c linux
2条回答
一纸荒年 Trace。
2楼-- · 2019-09-06 09:50

In your call to ftw, the first parameter is mRootFilePath.c_str. Perhaps you want mRootFilePath.c_str() instead?

查看更多
Anthone
3楼-- · 2019-09-06 09:52

Aside from Managu's answer, don't forget to make sure that your static member function uses C linkage (extern "C") - depending on the implementation, you may not be able to pass a pointer to a function with the default C++ linkage where a pointer to C function is expected (when ABIs differ).

查看更多
登录 后发表回答