【发布时间】:2020-10-19 11:01:27
【问题描述】:
我是 C++ 编码的新手,我遇到了这个问题: 我想做这样的事情
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <ftw.h>
class Foo {
public:
int func1 (const char *fpath, const struct stat *sb, int typeflag){
total += sb->st_size;
std::cout << total << std::endl;
return 0;
}
int func2(){
ftw("plots", func1, 3);
}
int total;
};
是否有解决 func1 不是静态的事实的方法?我还尝试在 func2() 中定义一个 lambda(或 std::function),以便将其传递给 ftw(),但它需要 int (*)(const char*, const stat*, int) 作为第二个参数,因此它不起作用。
提前致谢。
【问题讨论】:
-
更广泛的背景是什么?我对
ftw一无所知。 -
在 C++17 中你可以使用
<filesystem>。 en.cppreference.com/w/cpp/filesystem/directory_iterator 如果你被 C 卡住了,你不能传递成员函数或 lambdas。
标签: c++ non-static