【问题标题】:boost::filesystem::path and std::stringboost::filesystem::path 和 std::string
【发布时间】:2011-04-23 17:55:36
【问题描述】:

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

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

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

但事实证明 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&amp; s)
  • 但是,它仍然提出了为什么会出现错误的问题。因为它似乎可以传递一个副本,因为它可以直接分配。但不知何故,它在 中丢失了
  • @Sam 很公平;我不是很清楚。我想知道您的处方是否与手头的错误有关。

标签: c++ boost path boost-filesystem


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 2012-07-06
    • 2016-06-02
    • 2012-01-23
    • 1970-01-01
    • 2017-12-30
    • 2018-01-16
    相关资源
    最近更新 更多