使用native()boost路径访问器

时间:2014-01-05 19:00:19

标签: c++ gcc boost boost-filesystem

我收到此错误:

luascript.cpp: In member function ‘bool LuaInterface::loadDirectory(const string&, Npc*, bool)’:
luascript.cpp:744:69: error: no matching function for call to ‘LuaInterface::loadDirectory(boost::filesystem3::path, Npc*&, bool&)’
luascript.cpp:744:69: note: candidate is:
luascript.cpp:736:6: note: bool LuaInterface::loadDirectory(const string&, Npc*, bool)
luascript.cpp:736:6: note:   no known conversion for argument 1 from ‘boost::filesystem3::path’ to ‘const string& {aka const std::basic_string<char>&}’

这段代码:

    bool LuaInterface::loadDirectory(const std::string& dir, Npc* npc/* = NULL*/, bool recursively/* = false*/)
{
    StringVec files;
    for(boost::filesystem::directory_iterator it(dir), end; it != end; ++it)
    {
        std::string s = it->path().filename().string();
        if(boost::filesystem::is_directory(it->status()))
        {
            if(recursively && !loadDirectory(it->path() / s, npc, recursively))
            return false;
        }
        else if((s.size() > 4 ? s.substr(s.size() - 4) : "") == ".lua")
            files.push_back(s);
    }

    std::sort(files.begin(), files.end());
    for(StringVec::iterator it = files.begin(); it != files.end(); ++it)
    {
        if(!loadFile(dir + (*it), npc))
            return false;
    }

    return true;
}

我改变了第一个如果要像这里建议的更干净,但他们也告诉我使用native()路径加速器。

1 个答案:

答案 0 :(得分:3)

然后......你为什么不呢? :)

if(recursively && !loadDirectory((it->path() / s).native(), npc, recursively))

到目前为止,

可能有意义
  • it->path() / s
  • 引入变量
  • 阅读documentation for Boost Filesystem
    代码建议您尝试递归遍历文件系统树。你看过boost::recursive_directory_iterator吗?