1. <i id='jnZcG'><tr id='jnZcG'><dt id='jnZcG'><q id='jnZcG'><span id='jnZcG'><b id='jnZcG'><form id='jnZcG'><ins id='jnZcG'></ins><ul id='jnZcG'></ul><sub id='jnZcG'></sub></form><legend id='jnZcG'></legend><bdo id='jnZcG'><pre id='jnZcG'><center id='jnZcG'></center></pre></bdo></b><th id='jnZcG'></th></span></q></dt></tr></i><div id='jnZcG'><tfoot id='jnZcG'></tfoot><dl id='jnZcG'><fieldset id='jnZcG'></fieldset></dl></div>

      <small id='jnZcG'></small><noframes id='jnZcG'>

        <bdo id='jnZcG'></bdo><ul id='jnZcG'></ul>

        <legend id='jnZcG'><style id='jnZcG'><dir id='jnZcG'><q id='jnZcG'></q></dir></style></legend>
      1. <tfoot id='jnZcG'></tfoot>

        为什么stack&lt;const string&gt;不能在 g++ 中编译

        时间:2023-05-24

                <i id='slGSq'><tr id='slGSq'><dt id='slGSq'><q id='slGSq'><span id='slGSq'><b id='slGSq'><form id='slGSq'><ins id='slGSq'></ins><ul id='slGSq'></ul><sub id='slGSq'></sub></form><legend id='slGSq'></legend><bdo id='slGSq'><pre id='slGSq'><center id='slGSq'></center></pre></bdo></b><th id='slGSq'></th></span></q></dt></tr></i><div id='slGSq'><tfoot id='slGSq'></tfoot><dl id='slGSq'><fieldset id='slGSq'></fieldset></dl></div>

                • <bdo id='slGSq'></bdo><ul id='slGSq'></ul>
                    <tbody id='slGSq'></tbody>
                • <tfoot id='slGSq'></tfoot>
                  <legend id='slGSq'><style id='slGSq'><dir id='slGSq'><q id='slGSq'></q></dir></style></legend>

                • <small id='slGSq'></small><noframes id='slGSq'>

                  本文介绍了为什么stack&lt;const string&gt;不能在 g++ 中编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我在为大型(相对于我们团队的规模)项目维护端口时遇到了这个问题,但是创建一个小示例很简单.stackoverflow.cpp:

                  I encountered this problem maintaining a port for a large (relative to the size of our team) project, but it was simple to create a small example. stackoverflow.cpp:

                  #include<iostream>
                  #include<string>
                  #include<stack>
                  
                  using namespace std;
                  
                  
                  int main (int argc, char *argv[]) {
                    stack<const string> strstack;
                    string str("Hello, world");
                    strstack.push(str);
                    cout << strstack.top() << endl;
                    return 0;
                  }
                  

                  看起来正确,对吧?MSVS 也这么认为.然而:

                  Looks correct, right? MSVS thinks so too. However:

                  g++ stackoverflow.cpp
                  In file included from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34:0,
                                   from /usr/include/c++/4.7/bits/allocator.h:48,
                                   from /usr/include/c++/4.7/string:43,
                                   from /usr/include/c++/4.7/bits/locale_classes.h:42,
                                   from /usr/include/c++/4.7/bits/ios_base.h:43,
                                   from /usr/include/c++/4.7/ios:43,
                                   from /usr/include/c++/4.7/ostream:40,
                                   from /usr/include/c++/4.7/iostream:40,
                                   from stackoverflow.cpp:1:
                  /usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘class __gnu_cxx::new_allocator<const std::basic_string<char> >’:
                  /usr/include/c++/4.7/bits/allocator.h:89:11:   required from ‘class std::allocator<const std::basic_string<char> >’
                  /usr/include/c++/4.7/bits/stl_deque.h:489:61:   required from ‘class std::_Deque_base<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >’
                  /usr/include/c++/4.7/bits/stl_deque.h:728:11:   required from ‘class std::deque<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >’
                  /usr/include/c++/4.7/bits/stl_stack.h:98:46:   required from ‘class std::stack<const std::basic_string<char> >’
                  stackoverflow.cpp:9:23:   required from here
                  /usr/include/c++/4.7/ext/new_allocator.h:83:7: error: ‘const _Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::const_reference) const [with _Tp = const std::basic_string<char>; __gnu_cxx::new_allocator<_Tp>::const_pointer = const std::basic_string<char>*; __gnu_cxx::new_allocator<_Tp>::const_reference = const std::basic_string<char>&]’ cannot be overloaded
                  /usr/include/c++/4.7/ext/new_allocator.h:79:7: error: with ‘_Tp* __gnu_cxx::new_allocator<_Tp>::address(__gnu_cxx::new_allocator<_Tp>::reference) const [with _Tp = const std::basic_string<char>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::basic_string<char>*; __gnu_cxx::new_allocator<_Tp>::reference = const std::basic_string<char>&]’
                  /usr/include/c++/4.7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::deallocate(__gnu_cxx::new_allocator<_Tp>::pointer, __gnu_cxx::new_allocator<_Tp>::size_type) [with _Tp = const std::basic_string<char>; __gnu_cxx::new_allocator<_Tp>::pointer = const std::basic_string<char>*; __gnu_cxx::new_allocator<_Tp>::size_type = long unsigned int]’:
                  /usr/include/c++/4.7/bits/stl_deque.h:540:2:   required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_deallocate_node(_Tp*) [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
                  /usr/include/c++/4.7/bits/stl_deque.h:643:2:   required from ‘void std::_Deque_base<_Tp, _Alloc>::_M_destroy_nodes(_Tp**, _Tp**) [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
                  /usr/include/c++/4.7/bits/stl_deque.h:566:4:   required from ‘std::_Deque_base<_Tp, _Alloc>::~_Deque_base() [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
                  /usr/include/c++/4.7/bits/stl_deque.h:781:15:   required from ‘std::deque<_Tp, _Alloc>::deque() [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >]’
                  stackoverflow.cpp:9:23:   required from here
                  /usr/include/c++/4.7/ext/new_allocator.h:100:9: error: invalid conversion from ‘const void*’ to ‘void*’ [-fpermissive]
                  In file included from /usr/include/c++/4.7/ext/new_allocator.h:34:0,
                                   from /usr/include/c++/4.7/x86_64-linux-gnu/bits/c++allocator.h:34,
                                   from /usr/include/c++/4.7/bits/allocator.h:48,
                                   from /usr/include/c++/4.7/string:43,
                                   from /usr/include/c++/4.7/bits/locale_classes.h:42,
                                   from /usr/include/c++/4.7/bits/ios_base.h:43,
                                   from /usr/include/c++/4.7/ios:43,
                                   from /usr/include/c++/4.7/ostream:40,
                                   from /usr/include/c++/4.7/iostream:40,
                                   from stackoverflow.cpp:1:
                  /usr/include/c++/4.7/new:97:6: error:   initializing argument 1 of ‘void operator delete(void*)’ [-fpermissive]
                  In file included from /usr/include/c++/4.7/deque:63:0,
                                   from /usr/include/c++/4.7/stack:61,
                                   from stackoverflow.cpp:3:
                  /usr/include/c++/4.7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, const _T2&) [with _T1 = const std::basic_string<char>; _T2 = std::basic_string<char>]’:
                  /usr/include/c++/4.7/bits/stl_uninitialized.h:77:3:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _ForwardIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; bool _TrivialValueTypes = false]’
                  /usr/include/c++/4.7/bits/stl_uninitialized.h:119:41:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _ForwardIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>]’
                  /usr/include/c++/4.7/bits/stl_uninitialized.h:260:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _ForwardIterator = std::_Deque_iterator<const std::basic_string<char>, const std::basic_string<char>&, const std::basic_string<char>*>; _Tp = const std::basic_string<char>]’
                  /usr/include/c++/4.7/bits/stl_deque.h:841:9:   required from ‘std::deque<_Tp, _Alloc>::deque(const std::deque<_Tp, _Alloc>&) [with _Tp = const std::basic_string<char>; _Alloc = std::allocator<const std::basic_string<char> >; std::deque<_Tp, _Alloc> = std::deque<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >]’
                  /usr/include/c++/4.7/bits/stl_stack.h:130:14:   required from ‘std::stack<_Tp, _Sequence>::stack(const _Sequence&) [with _Tp = const std::basic_string<char>; _Sequence = std::deque<const std::basic_string<char>, std::allocator<const std::basic_string<char> > >]’
                  stackoverflow.cpp:9:23:   required from here
                  /usr/include/c++/4.7/bits/stl_construct.h:85:7: error: invalid static_cast from type ‘const std::basic_string<char>*’ to type ‘void*’
                  

                  使用 stackstrstack; 一切都很干净.这是一个g ++错误吗?是否有解决方法,以便我们在修复错误之前不必删除 const?

                  With stack<string> strstack; it's all clean. Is this a g++ bug? Is there a workaround so that we won't have to drop the const until the bug is fixed?

                  推荐答案

                  标准容器的成员必须是可复制或可移动的 (C++11).如果类型是 const 则不符合要求.

                  The members of a standard container have to be copy assignable or movable (C++11). If the type is const it fails the requirements.

                  这篇关于为什么stack&lt;const string&gt;不能在 g++ 中编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何让 std::make_unique 成为我班的朋友 下一篇:C++ 模板函数默认值

                  相关文章

                  最新文章

                  1. <i id='hbrZW'><tr id='hbrZW'><dt id='hbrZW'><q id='hbrZW'><span id='hbrZW'><b id='hbrZW'><form id='hbrZW'><ins id='hbrZW'></ins><ul id='hbrZW'></ul><sub id='hbrZW'></sub></form><legend id='hbrZW'></legend><bdo id='hbrZW'><pre id='hbrZW'><center id='hbrZW'></center></pre></bdo></b><th id='hbrZW'></th></span></q></dt></tr></i><div id='hbrZW'><tfoot id='hbrZW'></tfoot><dl id='hbrZW'><fieldset id='hbrZW'></fieldset></dl></div>

                      • <bdo id='hbrZW'></bdo><ul id='hbrZW'></ul>

                      <legend id='hbrZW'><style id='hbrZW'><dir id='hbrZW'><q id='hbrZW'></q></dir></style></legend>

                    1. <small id='hbrZW'></small><noframes id='hbrZW'>

                      <tfoot id='hbrZW'></tfoot>