【发布时间】:2011-05-20 15:06:35
【问题描述】:
boost 网站上的示例代码不起作用。 http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/tutorial.html#Using-path-decomposition
int main(int argc, char* argv[])
{
path p (argv[1]); // p reads clearer than argv[1] in the following code
try
{
if (exists(p)) // does p actually exist?
{
if (is_regular_file(p)) // is p a regular file?
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p)) // is p a directory?
{
cout << p << " is a directory containing:\n";
typedef vector<path> vec; // store paths,
vec v; // so we can sort them later
copy(directory_iterator(p), directory_iterator(), back_inserter(v));
sort(v.begin(), v.end()); // sort, since directory iteration
// is not ordered on some file systems
for (vec::const_iterator it (v.begin()); it != v.end(); ++it)
{
path fn = it->path().filename(); // extract the filename from the path
v.push_back(fn); // push into vector for later sorting
}
}
else
cout << p << " exists, but is neither a regular file nor a directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
在 Visual Studio 2010 中为 path fn = it->path().filename(); 行编译时出现两条错误消息
第一个错误是:'function-style cast' : illegal as right side of '->' operator,第二个错误是:left of '.filename' must have class/struct/union
此外,当我将鼠标悬停在 path() 上时,它会显示:class boost::filesystem3::path Error: typename not allowed
【问题讨论】:
-
你能把你找到这段代码的网址贴出来吗?
-
boost 文件系统在某些时候发生了变化。标头将包含#define BOOST_FILESYSTEM_VERSION,其中 boost::filesystem::path::filename() 的返回类型从 boost 版本 2 更改为 3。我有一些带有 #if #else 的代码,在版本 3 中,调用 .string() 使两个版本匹配