gpt4 book ai didi

c++ - 将一个 vector 复制到其他 C++ 时出错

转载 作者:行者123 更新时间:2023-11-28 00:10:46 25 4
gpt4 key购买 nike

我已经为大多数 STL 容器重载了分配器,下面是代码。代码来自here

#include <iostream>
#include <vector>
#include <list>

namespace outside
{
template<typename T>
struct MyAllocator
{
typedef typename std::allocator<T>::value_type value_type;
typedef typename std::allocator<T>::pointer pointer;
typedef typename std::allocator<T>::const_pointer const_pointer;
typedef typename std::allocator<T>::reference reference;
typedef typename std::allocator<T>::const_reference const_reference;
typedef typename std::allocator<T>::size_type size_type;
pointer allocate (size_type n, typename std::allocator<void>::const_pointer hint=0)
{
std::cerr << "allocate..." << std::endl;
return std::allocator<T>{}.allocate(n, hint);
}
void deallocate (pointer p, size_type n)
{
std::cerr << "deallocate..." << std::endl;
return std::allocator<T>{}.deallocate(p, n);
}
template <class Type> struct rebind
{
typedef MyAllocator<Type> other;
};
MyAllocator()
{
}
MyAllocator(const MyAllocator<T>& other )
{
}
template< class U >
MyAllocator( const MyAllocator<U>& other )
{
}
};
} // namespace outside

namespace outer
{
template<class T>
using vector = ::std::vector<T, outside::MyAllocator<T>>;

template<class T>
using list = ::std::list<T, outside::MyAllocator<T>>;
}


int main()
{
outer::vector<int> myVector1;
outer::vector<int> myVector2;
myVector1.push_back(10);
myVector1.push_back(20);
// myVector2 = myVector1; // Getting compilation error
for (auto i = myVector1.begin(); i != myVector1.end(); ++i) {
std::cout << *i << ' ';
}

std::list<int> myList1;
std::list<int> myList2;
myList2 = myList1; // // No compilation error
}

将一个 vector 复制到另一个 vector 会出错。虽然对于相同的代码,将一个列表复制到另一个列表工作正常。

编译错误

included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc: In instantiation of 'std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = int; _Alloc = outside::MyAllocator<int>]':
prog.cpp:59:15: required from here
/usr/include/c++/5/bits/vector.tcc:176:37: error: no match for 'operator!=' (operand types are 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' and 'const _Tp_alloc_type {aka const outside::MyAllocator<int>}')
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/iosfwd:40:0,
from /usr/include/c++/5/ios:38,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/postypes.h:221:5: note: candidate: template<class _StateT> bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
operator!=(const fpos<_StateT>& __lhs, const fpos<_StateT>& __rhs)
^
/usr/include/c++/5/bits/postypes.h:221:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::fpos<_StateT>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:64:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_pair.h:227:5: note: candidate: template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^
/usr/include/c++/5/bits/stl_pair.h:227:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::pair<_T1, _T2>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:304:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator!=(const reverse_iterator<_Iterator>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:304:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::reverse_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:354:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
operator!=(const reverse_iterator<_IteratorL>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:354:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::reverse_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1077:5: note: candidate: template<class _IteratorL, class _IteratorR> bool std::operator!=(const std::move_iterator<_Iterator>&, const std::move_iterator<_IteratorR>&)
operator!=(const move_iterator<_IteratorL>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:1077:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::move_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/stl_algobase.h:67:0,
from /usr/include/c++/5/bits/char_traits.h:39,
from /usr/include/c++/5/ios:40,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/stl_iterator.h:1083:5: note: candidate: template<class _Iterator> bool std::operator!=(const std::move_iterator<_Iterator>&, const std::move_iterator<_Iterator>&)
operator!=(const move_iterator<_Iterator>& __x,
^
/usr/include/c++/5/bits/stl_iterator.h:1083:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::move_iterator<_Iterator>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:41:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/allocator.h:140:5: note: candidate: template<class _T1, class _T2> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_T2>&)
operator!=(const allocator<_T1>&, const allocator<_T2>&)
^
/usr/include/c++/5/bits/allocator.h:140:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::allocator<_CharT>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:41:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/allocator.h:146:5: note: candidate: template<class _Tp> bool std::operator!=(const std::allocator<_CharT>&, const std::allocator<_CharT>&)
operator!=(const allocator<_Tp>&, const allocator<_Tp>&)
^
/usr/include/c++/5/bits/allocator.h:146:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::allocator<_CharT>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4948:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/5/bits/basic_string.h:4948:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4960:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
operator!=(const _CharT* __lhs,
^
/usr/include/c++/5/bits/basic_string.h:4960:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: mismatched types 'const _CharT*' and 'outside::MyAllocator<int>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/string:52:0,
from /usr/include/c++/5/bits/locale_classes.h:40,
from /usr/include/c++/5/bits/ios_base.h:41,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/basic_string.h:4972:5: note: candidate: template<class _CharT, class _Traits, class _Alloc> bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
operator!=(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
^
/usr/include/c++/5/bits/basic_string.h:4972:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::basic_string<_CharT, _Traits, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/bits/ios_base.h:46:0,
from /usr/include/c++/5/ios:42,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/system_error:311:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_code&)
operator!=(const error_code& __lhs, const error_code& __rhs) noexcept
^
/usr/include/c++/5/system_error:311:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_code&'
/usr/include/c++/5/system_error:315:3: note: candidate: bool std::operator!=(const std::error_code&, const std::error_condition&)
operator!=(const error_code& __lhs, const error_condition& __rhs) noexcept
^
/usr/include/c++/5/system_error:315:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_code&'
/usr/include/c++/5/system_error:319:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_code&)
operator!=(const error_condition& __lhs, const error_code& __rhs) noexcept
^
/usr/include/c++/5/system_error:319:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_condition&'
/usr/include/c++/5/system_error:323:3: note: candidate: bool std::operator!=(const std::error_condition&, const std::error_condition&)
operator!=(const error_condition& __lhs,
^
/usr/include/c++/5/system_error:323:3: note: no known conversion for argument 1 from 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' to 'const std::error_condition&'
In file included from /usr/include/c++/5/bits/locale_facets.h:48:0,
from /usr/include/c++/5/bits/basic_ios.h:37,
from /usr/include/c++/5/ios:44,
from /usr/include/c++/5/ostream:38,
from /usr/include/c++/5/iostream:39,
from prog.cpp:1:
/usr/include/c++/5/bits/streambuf_iterator.h:210:5: note: candidate: template<class _CharT, class _Traits> bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
^
/usr/include/c++/5/bits/streambuf_iterator.h:210:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::istreambuf_iterator<_CharT, _Traits>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/vector:64:0,
from prog.cpp:2:
/usr/include/c++/5/bits/stl_vector.h:1535:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator!=(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/include/c++/5/bits/stl_vector.h:1535:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::vector<_Tp, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/list:63:0,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_list.h:291:5: note: candidate: template<class _Val> bool std::operator!=(const std::_List_iterator<_Tp>&, const std::_List_const_iterator<_Val>&)
operator!=(const _List_iterator<_Val>& __x,
^
/usr/include/c++/5/bits/stl_list.h:291:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::_List_iterator<_Tp>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^
In file included from /usr/include/c++/5/list:63:0,
from prog.cpp:3:
/usr/include/c++/5/bits/stl_list.h:1843:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator!=(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
operator!=(const list<_Tp, _Alloc>& __x, const list<_Tp, _Alloc>& __y)
^
/usr/include/c++/5/bits/stl_list.h:1843:5: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/5/vector:69:0,
from prog.cpp:2:
/usr/include/c++/5/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::list<_Tp, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^

我想了解为什么会出现这种情况以及解决此问题的方法?

最佳答案

异常的关键部分是其中的数百行:

In file included from /usr/local/include/c++/5.2.0/vector:64:0,
from main.cpp:2: /usr/local/include/c++/5.2.0/bits/stl_vector.h:1535:5: note: candidate: template<class _Tp, class _Alloc> bool std::operator!=(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
operator!=(const vector<_Tp, _Alloc>& __x, const vector<_Tp, _Alloc>& __y)
^
/usr/local/include/c++/5.2.0/bits/stl_vector.h:1535:5: note: template argument deduction/substitution failed:
In file included from /usr/local/include/c++/5.2.0/vector:69:0,
from main.cpp:2:
/usr/local/include/c++/5.2.0/bits/vector.tcc:176:37: note: 'std::_Vector_base<int, outside::MyAllocator<int> >::_Tp_alloc_type {aka outside::MyAllocator<int>}' is not derived from 'const std::vector<_Tp, _Alloc>'
&& _M_get_Tp_allocator() != __x._M_get_Tp_allocator())
^

vector::operator= 需要 vector::operator!=,后者需要 allocator::operator!=,而后者不需要存在。没有它,就无法创建函数。只需将 operator!= 添加到您的分配器即可。当您使用它时,还要确保您拥有所有其他必需的分配器函数。如果没记错的话,你漏掉了很多。

关于c++ - 将一个 vector 复制到其他 C++ 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33265641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com