3

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

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

我现在有以字符串为参数的函数,例如 Load(String path);

但事实证明 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);
        }
4

1 回答 1

4

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

于 2010-10-11T18:50:48.383 回答