c++ - 包含字符串时出现 STL_algobase.h 错误

标签 c++ stl g++

在最近创建的项目中,我在尝试 make 时遇到此错误:

备注:params.h:11只是#include <string>

Building file: ../src/cmds/params.cpp
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/cmds/params.d" -MT"src/cmds/params.d" -o "src/cmds/params.o" "../src/cmds/params.cpp"
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/string:40,
                 from ../src/cmds/params.h:11,
                 from ../src/cmds/params.cpp:8:
/usr/include/c++/4.8/bits/stl_algobase.h: In instantiation of ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = char*; _OI = std::ostream_iterator<std::basic_string<char> >]’:
/usr/include/c++/4.8/bits/stl_algobase.h:390:70:   required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = char*; _OI = std::ostream_iterator<std::basic_string<char> >]’
/usr/include/c++/4.8/bits/stl_algobase.h:428:38:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _OI = std::ostream_iterator<std::basic_string<char> >]’
/usr/include/c++/4.8/bits/stl_algobase.h:460:17:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; _OI = std::ostream_iterator<std::basic_string<char> >]’
../src/cmds/params.cpp:96:81:   required from here
/usr/include/c++/4.8/bits/stl_algobase.h:335:18: error: invalid user-defined conversion from ‘char’ to ‘const std::basic_string<char>&’ [-fpermissive]
        *__result = *__first;
                  ^
In file included from /usr/include/c++/4.8/string:53:0,
                 from ../src/cmds/params.h:11,
                 from ../src/cmds/params.cpp:8:
/usr/include/c++/4.8/bits/basic_string.tcc:212:5: note: candidate is: std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>] <near match>
     basic_string<_CharT, _Traits, _Alloc>::
     ^
/usr/include/c++/4.8/bits/basic_string.tcc:212:5: note:   no known conversion for argument 1 from ‘char’ to ‘const char*’
In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.8/string:40,
                 from ../src/cmds/params.h:11,
                 from ../src/cmds/params.cpp:8:
/usr/include/c++/4.8/bits/stl_algobase.h:335:18: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
        *__result = *__first;
                  ^
In file included from /usr/include/c++/4.8/string:53:0,
                 from ../src/cmds/params.h:11,
                 from ../src/cmds/params.cpp:8:
/usr/include/c++/4.8/bits/basic_string.tcc:212:5: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-fpermissive]
     basic_string<_CharT, _Traits, _Alloc>::
     ^
make: *** [src/cmds/params.o] Error 1

可能是什么原因?

最佳答案

下面的错误在这里很重要

    /usr/include/c++/4.8/bits/stl_algobase.h:335:18: 
error: invalid user-defined conversion from ‘char’ to ‘const std::basic_string<char>&’ [-fpermissive]
            *__result = *__first;

这表明在您的程序中某处您正在尝试将 char 转换为 std::string不可能。

关于c++ - 包含字符串时出现 STL_algobase.h 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23305567/

相关文章:

c++ - 通过调用 C++ 中的静态类函数初始化的全局静态变量

C++11 std::to_string(double) - 没有尾随零

c++ - 如果容器不是调用函数中的引用,则使用 std::thread 传递对迭代器的引用失败

c++ - 需要将 Glib::RefPtr<T> 转换或转换为 Gtk::Widget&

C++空程序内存泄漏

c++ - Cocos2d Portrait Mode 对象位置搞砸了

c++ - ifstream get() 不填充字符数组

C++ 字符串 size_t

c++ - 从文件中读取时遇到问题。似乎过早地达到了 EOF

c++ - c++ vector 中元素的划分