gpt4 book ai didi

c++ - Boost directory_iterator : 'begin' and 'end' not declared

转载 作者:行者123 更新时间:2023-11-30 02:28:55 27 4
gpt4 key购买 nike

我正在尝试学习如何使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com