VS2015无法从'initializer list'转换为'std::string'错误


// get the drive letter/network volume portion of the path
    std::string drive_path;
    if (drive_type == disk_util::drive_type_network)
    {
        boost::iterator_range<std::string::const_iterator> r = boost::find_nth(path, "/", 3);
        if (!r.empty())
            drive_path = std::string(path.begin(), r.begin());
    }
    else
    {
        size_t i = path.find('/');
        drive_path = path.substr(0, i);
    }

//path is also a std::string type 

问题出现在线: drive_path = std :: string(path.begin(),r.begin());

这是在VS2013上成功编译的,但在VS 2015中丢了错误错误:错误C2440:'':无法从"初始化器列表"转换为'std :: String'

根据std ::字符串构造函数,我们有一个范围构造函数,该范围构造函数将iTerator1和迭代器2填充字符串。

http://www.cplusplus.com/reference/string/string/string/

    //range (7) 
template <class InputIterator>
  string  (InputIterator first, InputIterator last);

在VS2013中,它现在显示出任何潜在问题的警告并成功地编译了。

两个迭代器都是相同的字符串,我不知道为什么在VS2015中失败。任何帮助都将受到赞赏。

string ctor都需要同一类型class InputIterator的迭代器。您的path.begin()std::string::iterator,而r.begin()std::string::const_iterator

我会尝试一个(两个const)

drive_path = std::string(path.cbegin(), r.begin());

或(同时进行非con)

boost::iterator_range<std::string::iterator> r;

迭代参数应为您使用的字符串构造函数的类型相同。
我怀疑您是将" non-const"迭代器(path.begin())作为第一个参数,而const_iterator则是第二个。

您可以尝试通过两个const_iterator(使用String :: cbegin()):

drive_path = std::string(path.cbegin(), r.begin());

最新更新