c++ - boost::filesystem::path 和 std::string

标签 c++ boost path boost-filesystem

我有一个 String 类,它有一个成员 std::string。构造函数之一是

String (std::string s)
{
    // member: std::string _mString;
    _mString = s;  // error on path assignment
}

我现在有以 String 作为参数的函数,例如加载(字符串路径);

但事实证明 boost::filesystem::path::string() 与那个 String 构造函数不兼容,但是,通常分配是可以的

boost::filesystem::path somepath("some directory")
std::string filename = somepath.extension(); // OK!

发生了什么事?我怎样才能让我的构造函数工作?谢谢。

编辑:通过将其设置为 const ref 解决了问题,但仍然很好奇为什么会出现错误,因为传递拷贝似乎没问题,因为它可以直接分配。 文件 xstring 错误

void __CLR_OR_THIS_CALL _Tidy(bool _Built = false,
        size_type _Newsize = 0)
        {   // initialize buffer, deallocating any storage
        if (!_Built)
            ;
        else if (_BUF_SIZE <= _Myres)
            {   // copy any leftovers to small buffer and deallocate
            _Elem *_Ptr = _Bx._Ptr;
            if (0 < _Newsize)
                _Traits_helper::copy_s<_Traits>(_Bx._Buf, _BUF_SIZE, _Ptr, _Newsize);
            _Mybase::_Alval.deallocate(_Ptr, _Myres + 1);
            }
        _Myres = _BUF_SIZE - 1; // **** ERROR ***
        _Eos(_Newsize);
        }

最佳答案

在你的构造函数中:String (std::string s) 应该是 String (const std::string& s)

关于c++ - boost::filesystem::path 和 std::string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3905280/

相关文章:

c# - 限制文件/文件夹写入指定根以外的路径

c++ - 使用 C++ 将文件和目录从一个目录递归复制到另一个目录时,不会保留软链接(soft link)

c++ - 复制构造函数的奇怪行为

c++ - 局部变量和作为参数传递的变量

c++ - Boost.Asio IPv6 为什么绑定(bind)错误?

c# - 你能从 StreamReader 对象中获取文件路径吗?

c++ - 什么是 AoB(字节数组)

c++ - 使用 OpenMP 在 odeint 中创建受控步进器

c++ - CMake Find.Boost 不考虑配置

python - Rpy2 找不到 R.dll