- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习如何使用 Boost C++ 库,但我遇到了一个问题。运行以下 boost::filesystem 教程代码时:
#include <iostream>
#include <boost/filesystem.hpp>
using std::cout;
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 path\n";
return 1;
}
path p (argv[1]);
try
{
if (exists(p))
{
if (is_regular_file(p))
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p))
{
cout << p << " is a directory containing:\n";
for (directory_entry& x : directory_iterator(p))
cout << " " << x.path() << '\n';
}
else
cout << p << " exists, but is not a regular file or directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
并编译:
g++ -std=c++11 main.cpp -o main -L$BOOST_ROOT -lboost_filesystem -lboost_system
或:
g++ -std=c++11 main.cpp -o main -L$BOOST_ROOT -lboost_system -lboost_filesystem
其中 BOOST_ROOT 是我提取 boost_1_62_0.tar.gz 的路径,我收到以下错误:
FoamData.cpp:36:76: error: ‘begin’ was not declared in this scope
for (auto&& x : boost::filesystem::directory_iterator(p))
^
FoamData.cpp:36:76: note: suggested alternative:
In file included from /usr/include/c++/5/vector:66:0,
from FoamData.hpp:4,
from FoamData.cpp:1:
/usr/include/c++/5/bits/range_access.h:87:5: note: ‘std::begin’
begin(_Tp (&__arr)[_Nm])
^
FoamData.cpp:36:76: error: ‘end’ was not declared in this scope
for (auto&& x : boost::filesystem::directory_iterator(p))
^
FoamData.cpp:36:76: note: suggested alternative:
In file included from /usr/include/c++/5/vector:66:0,
from FoamData.hpp:4,
from FoamData.cpp:1:
/usr/include/c++/5/bits/range_access.h:97:5: note: ‘std::end’
end(_Tp (&__arr)[_Nm])
^
make: *** [FoamData.o] Error 1
如果我掩盖了一些明显的事情,我提前道歉。
编辑
随后的 boost_filesystem 教程中的以下语句给出了相同的错误:
for(auto&& x : directory_iterator(p))
代码来源: boost::filesystem tutorials
更新
我重新编译了 boost_1_62_0 并运行了教程脚本来构建教程代码(根据 boost 文件系统教程)。构建的教程程序可以正常工作。但是,在 $BOOST_ROOT
之外使用 g++ 进行编译时,出现以下错误:
./tester:加载共享库时出错:libboost_system.so.1.62.0:无法打开共享对象文件:没有这样的文件或目录
下面是我的makefile
:
CC=g++
INCLUDE=-I/usr/local/boost_1_62_0
CFLAGS=-std=c++11 -Wall -Wextra -g
LDFLAGS=-lboost_filesystem -lboost_system
LDLIB=-L/usr/local/boost_1_62_0/stage/lib
all: tester
tester: main.o
$(CC) main.o $(LDLIB) $(LDFLAGS) $(CFLAGS) -o tester
main.o: main.cpp
$(CC) -c main.cpp $(INCLUDE) $(CFLAGS)
clean:
rm *.o tester
最终更新
通过将此添加到我的 .bashrc
,我能够消除我的最后一个错误:
导出 LD_LIBRARY_PATH="$BOOST_ROOT/stage/lib:$LD_LIBRARY_PATH"
感谢大家的帮助!
最佳答案
很可能您的代码使用了错误的(和过时的)系统 Boost 库,其中 begin()
和 end()
成员未在 directory_iterator 中提供
。
原因是,在您的 g++ 命令行中,您使用 -L
提供了一个额外的库路径,但是您忘记使用 -I
添加一个额外的包含路径.
您可以像这样为 boost 版本添加编译时检查:
#include <boost/static_assert.hpp>
BOOST_STATIC_ASSERT(BOOST_VERSION >= 106200) // check for at least 1.62.0
关于c++ - Boost directory_iterator : 'begin' and 'end' not declared,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40241163/
自从我使用 C++ 以来已经有一段时间了,请原谅我的新手问题。 我编写了以下代码来获取目录内容的列表,它运行良好: for (directory_iterator end, dir("./");
我正在尝试遍历目录并读取其中的图像,同时允许跳过每第 n 个文件。 我的代码目前看起来像这样: // Count number of files in directory and reserve me
这是我的代码: #include #include int main(int argc, char *argv[]) { auto iter = std::filesystem::dire
编辑:我使用的是 std::tr2::sys 而不是 boost 的文件系统 我正在开发一个简单的应用程序,它可以扫描计算机上的目录并返回有关这些文件的统计信息。可以通过两种方式调用扫描:递归和非递归
我想重命名目录中的文件。目录中有 52 个文件夹。每个文件夹都有不同的名称,每个文件夹中有大约 40 个文件。我想提取特定文件夹的名称并将该名称附加到该特定文件夹中的文件名称。 当每个文件夹中只有 3
所以我写了一个小程序来试用 Boost Filesystem。我的程序将写入当前路径中有多少个文件,然后是文件名。这是我的程序: #include #include using namespace
我一直在尝试使用通过快速谷歌搜索找到的这个示例遍历目录: namespace bf = boost::filesystem; bf::path p("somedir"); bf::directory_
我已经在 directory_iterator page 上复制了示例代码,所以这就是我所拥有的: #include "pch.h" //for visual studios benefit #inc
我有以下代码: for (const auto& x : std::filesystem::directory_iterator(dir)) { // do stuff with x } di
我正在遍历一个目录,当某个项目符合某些条件时,我将其删除。我可以在循环内安全地执行此操作,还是必须将路径保存在数组中并稍后删除? 我在boost::filesystem docs中没有找到相关信息.
有些东西没有意义。根据我读过的内容,你使用 std::filesystem 是这样的: #include #include #include int main() { auto it
许多人(例如 1 、 2 )询问如何获得 std::filesystem::directory_iterator去工作,但在我读完这些之后我仍然遇到了麻烦。 我正在尝试构建一个小型静态库。在将目录迭代
我在 gcc 6.3.1 中使用实验性 std::filesystem 实现,遇到了一些关于 std::experimental::filesystem::directory_iterator #in
以下代码递归地遍历给定目录并每次以相同顺序打印其内容。 是否可以改变目录迭代器以随机方式打印目录内容(而不是使用 vector 存储结果然后随机打印 vector 内容)? #include #in
我正在编写一个程序,它获取目录中的所有文件名并将它们放入一个数组中。我遇到的问题是 operator++() 显示错误并且不会增加迭代器。任何帮助将不胜感激。 #include #include
我知道这听起来很愚蠢,但看看这个简单的例子(工作目录应该有多个项目): #define BOOST_FILESYSTEM_VERSION 3 #include #include int main(
我正在尝试使用其 insert(const_iterator pos, InputIt first, InputIt last) 将转换后的目录条目范围插入 vector 中成员函数模板。 不幸的是,
我正在尝试获取最后一个文件名。 下面的代码非常优雅。但它正在遍历目录中的所有文件。 是否可以在不迭代的情况下获取最后一个文件? #include std::string latestFileName
我正在使用 boost 的文件系统和 std::max_element() 来查找给定目录中名称最长的文件: #include #include #include #include #incl
我正在尝试学习如何使用 Boost C++ 库,但我遇到了一个问题。运行以下 boost::filesystem 教程代码时: #include #include using std::cout;
我是一名优秀的程序员,十分优秀!