Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的回调成员函数+;_C++_C_Linux - Fatal编程技术网

C++ c+中的回调成员函数+;

C++ c+中的回调成员函数+;,c++,c,linux,C++,C,Linux,当我编译这段代码时,会收到错误消息 class scanner { private: string mRootFilePath; static int AddToIndex( const char *,const struct stat *,int); public: scanner(string aRootFilePath){ mRootFil

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

    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:在成员函数“void scanner::Start()”中:
main.c:34:错误:类型为“const char*(std::basic_string::)()const”的参数与“const char*”不匹配

ftw函数在本例中调用回调函数AddToIndex()函数,该函数是类“scanner”的成员函数。。如何将此成员函数AddToIndex作为回调函数?如何解决这个问题

在对
ftw
的调用中,第一个参数是
mRootFilePath.c_str
。也许您希望使用
mRootFilePath.c_str()

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

除了Managu的答案之外,别忘了确保您的
static
成员函数使用c链接(
extern“c”
)-这取决于实现,您可能无法通过一个默认的C++链接传递一个函数,其中一个指针指向C函数(当ABIs不同时)。P> > P>除了Mangu的回答,别忘了确保你的<代码> static 成员函数使用C链接(<代码> Extn)C“< /COD>”-根据实现,你可能无法通过一个默认的C++链接传递一个函数,其中一个C函数的指针是预期的(当ABIs不同时)。p> @LiraLuna:这是一个著名的标准库函数。@LiraLuna:这是一个著名的标准库函数。
 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*’