не могу отправить свой объект в std :: vector - gcc не может найти что-то вроде copy-construct

0

Я использую такое поле:

std::vector<OneItemIndex> oneItemIndexes;

Это часть объявления OneItemIndex:

class OneItemIndex : public CustomIndex
{
public:
    OneItemIndex(int instrumentId_);
    ~OneItemIndex(void);
    OneItemIndex(OneItemIndex& rhs);
    ...

В VC++ это компилируется просто отлично, но в gcc я получаю такую ошибку компиляции:

In file included from /usr/include/c++/4.8/vector:62:0,
                 from ../IndexesStorage.h:4,
                 from ../IndexesStorage.cpp:1:
/usr/include/c++/4.8/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = OneItemIndex; _Args = {OneItemIndex}]:
/usr/include/c++/4.8/bits/stl_uninitialized.h:75:53:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<OneItemIndex*>; _ForwardIterator = OneItemIndex*; bool _TrivialValueTypes = false]
/usr/include/c++/4.8/bits/stl_uninitialized.h:117:41:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<OneItemIndex*>; _ForwardIterator = OneItemIndex*]
/usr/include/c++/4.8/bits/stl_uninitialized.h:258:63:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<OneItemIndex*>; _ForwardIterator = OneItemIndex*; _Tp = OneItemIndex]
/usr/include/c++/4.8/bits/stl_vector.h:1142:29:   required from ‘std::vector<_Tp, _Alloc>::pointer std::vector<_Tp, _Alloc>::_M_allocate_and_copy(std::vector<_Tp, _Alloc>::size_type, _ForwardIterator, _ForwardIterator) [with _ForwardIterator = std::move_iterator<OneItemIndex*>; _Tp = OneItemIndex; _Alloc = std::allocator<OneItemIndex>; std::vector<_Tp, _Alloc>::pointer = OneItemIndex*; std::vector<_Tp, _Alloc>::size_type = long unsigned int]
/usr/include/c++/4.8/bits/vector.tcc:75:70:   required from ‘void std::vector<_Tp, _Alloc>::reserve(std::vector<_Tp, _Alloc>::size_type) [with _Tp = OneItemIndex; _Alloc = std::allocator<OneItemIndex>; std::vector<_Tp, _Alloc>::size_type = long unsigned int]
../IndexesStorage.cpp:197:55:   required from here
/usr/include/c++/4.8/bits/stl_construct.h:75:7: error: no matching function for call to ‘OneItemIndex::OneItemIndex(OneItemIndex)
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^
/usr/include/c++/4.8/bits/stl_construct.h:75:7: note: candidates are:
In file included from ../IndexesStorage.h:7:0,
                 from ../IndexesStorage.cpp:1:
../OneItemIndex.h:13:2: note: OneItemIndex::OneItemIndex(OneItemIndex&)
  OneItemIndex(OneItemIndex& rhs);
  ^
../OneItemIndex.h:13:2: note:   no known conversion for argument 1 from ‘OneItemIndex to ‘OneItemIndex&
../OneItemIndex.h:8:2: note: OneItemIndex::OneItemIndex(int)
  OneItemIndex(int instrumentId_);
  ^
../OneItemIndex.h:8:2: note:   no known conversion for argument 1 from ‘OneItemIndex to ‘int

Почему gcc запрашивает OneItemIndex::OneItemIndex(OneItemIndex)? Должен ли я добавить такую конструкцию, как ее реализовать?

  • 7
    Параметр вашей копии-конструктора должны быть const .
  • 1
    Я ожидаю, что вы пытаетесь скопировать конструкцию OneItemIndex из временного. Вы не можете привязать временную ссылку к неконстантной ссылке.
Показать ещё 2 комментария
Теги:
gcc

1 ответ

2
Лучший ответ

+ Изменить

OneItemIndex(OneItemIndex& rhs);

в

OneItemIndex(OneItemIndex const& rhs);

в определении класса.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню