Strange error using a class array for a thread C++ - c++

I'm trying to do a Thread calling the cook class through a vector but I don't know to do it correctly. Here is my code:
for (int i = 0; i < _numCooks; i++) {
std::thread threadObj(this->_cooks[i].nextOrder, void);
_threads.push_back(threadObj);
}
_cooks is a vector of a class and the function that I am trying to make a thread of is:
bool cooks::nextOrder()
{
Margarita margarita(_cookOrder[0].pizzaSize);
Regina regina(_cookOrder[0].pizzaSize);
Fantasia fantasia(_cookOrder[0].pizzaSize);
Americana americana(_cookOrder[0].pizzaSize);
if (_cookOrder.empty())
return (false);
switch (_cookOrder[0].pizzaType) {
case plazza::Margarita:
return (makeOrder(margarita));
break;
case plazza::Regina:
return (makeOrder(regina));
break;
case plazza::Fantasia:
return (makeOrder(fantasia));
break;
case plazza::Americana:
return (makeOrder(americana));
break;
default:
return (false);
}
}
however when I try to compile I get this:
kitchen/kitchen.cpp: In member function ‘void
Kitchen::make_pizza(std::__cxx11::string, std::__cxx11::string, Ipc)’:
kitchen/kitchen.cpp:148:59: error: expected primary-expression before
‘void’
std::thread threadObj(this->_cooks[i].nextOrder, void);
^~~~ kitchen/kitchen.cpp:148:63: error: invalid use of non-static member
function ‘bool cooks::nextOrder()’
std::thread threadObj(this->_cooks[i].nextOrder, void);
^ In file included from kitchen/kitchen.hpp:17,
from kitchen/kitchen.cpp:5: kitchen/cooks.hpp:32:10: note: declared here
bool nextOrder();
^~~~~~~~~ In file included from /usr/include/c++/8/x86_64-redhat-linux/bits/c++allocator.h:33,
from /usr/include/c++/8/bits/allocator.h:46,
from /usr/include/c++/8/string:41,
from /usr/include/c++/8/bits/locale_classes.h:40,
from /usr/include/c++/8/bits/ios_base.h:41,
from /usr/include/c++/8/ios:42,
from /usr/include/c++/8/ostream:38,
from /usr/include/c++/8/iostream:39,
from kitchen/../pizza/IPizza.hpp:23,
from kitchen/../pizza/APizza.hpp:11,
from kitchen/kitchen.hpp:8,
from kitchen/kitchen.cpp:5: /usr/include/c++/8/ext/new_allocator.h: In instantiation of ‘void
__gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = cooks; _Args = {const cooks&}; _Tp = cooks]’: /usr/include/c++/8/bits/alloc_traits.h:475:4: required from ‘static
void std::allocator_traits
::construct(std::allocator_traits >::allocator_type&, _Up*, _Args&& ...) [with _Up = cooks; _Args = {const cooks&}; _Tp = cooks;
std::allocator_traits >::allocator_type =
std::allocator]’ /usr/include/c++/8/bits/stl_vector.h:1079:30:
required from ‘void std::vector<_Tp, _Alloc>::push_back(const
value_type&) [with _Tp = cooks; _Alloc = std::allocator;
std::vector<_Tp, _Alloc>::value_type = cooks]’
kitchen/kitchen.cpp:36:30: required from here
/usr/include/c++/8/ext/new_allocator.h:136:4: error: use of deleted
function ‘cooks::cooks(const cooks&)’ { ::new((void )__p)
_Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from kitchen/kitchen.hpp:17,
from kitchen/kitchen.cpp:5: kitchen/cooks.hpp:21:7: note: ‘cooks::cooks(const cooks&)’ is implicitly deleted because the
default definition would be ill-formed: class cooks
^~~~~ kitchen/cooks.hpp:21:7: error: use of deleted function ‘std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const
std::basic_ofstream<_CharT, _Traits>&) [with _CharT = char; _Traits =
std::char_traits]’ In file included from
kitchen/../plazza.hpp:15,
from kitchen/kitchen.hpp:13,
from kitchen/kitchen.cpp:5: /usr/include/c++/8/fstream:778:7: note: declared here
basic_ofstream(const basic_ofstream&) = delete;
^~~~~~~~~~~~~~ In file included from /usr/include/c++/8/vector:62,
from kitchen/../pizza/IPizza.hpp:24,
from kitchen/../pizza/APizza.hpp:11,
from kitchen/kitchen.hpp:8,
from kitchen/kitchen.cpp:5: /usr/include/c++/8/bits/stl_construct.h: In instantiation of ‘void
std::_Construct(_T1, _Args&& ...) [with _T1 = cooks; _Args =
{cooks}]’: /usr/include/c++/8/bits/stl_uninitialized.h:83:18:
required from ‘static _ForwardIterator
std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*; bool
_TrivialValueTypes = false]’ /usr/include/c++/8/bits/stl_uninitialized.h:134:15: required from
‘_ForwardIterator std::uninitialized_copy(_InputIterator,
_InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*]’
/usr/include/c++/8/bits/stl_uninitialized.h:289:37: required from
‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator,
_InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator; _ForwardIterator = cooks*; _Tp = cooks]’
/usr/include/c++/8/bits/stl_uninitialized.h:311:2: required from
‘_ForwardIterator
std::__uninitialized_move_if_noexcept_a(_InputIterator,
_InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = cooks*; _ForwardIterator = cooks*; _Allocator =
std::allocator]’ /usr/include/c++/8/bits/vector.tcc:447:6:
required from ‘void std::vector<_Tp,
_Alloc>::_M_realloc_insert(std::vector<_Tp, _Alloc>::iterator, _Args&& ...) [with _Args = {const cooks&}; _Tp = cooks; _Alloc =
std::allocator; std::vector<_Tp, _Alloc>::iterator =
__gnu_cxx::__normal_iterator >; typename std::_Vector_base<_Tp, _Alloc>::pointer = cooks*]’
/usr/include/c++/8/bits/stl_vector.h:1085:4: required from ‘void
std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp =
cooks; _Alloc = std::allocator; std::vector<_Tp,
_Alloc>::value_type = cooks]’ kitchen/kitchen.cpp:36:30: required from here /usr/include/c++/8/bits/stl_construct.h:75:7: error: use of
deleted function ‘cooks::cooks(const cooks&)’
{ ::new(static_cast(__p)) _T1(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [: kitchen/kitchen.o] Error 1
Is there any way to fix it?

According to this, the first argument to a std::thread constructor should meet the Callable concept. You then pass the subsequent arguments for the Callable as additional arguments to the constructor.
You're passing a member function. You need an instance of the class to call the method on, which you're not providing. This is what this error should mean to you.
error: invalid use of non-static member function ‘bool cooks::nextOrder()’
It's probably easiest to use a lambda ie.
for (int i = 0; i < _numCooks; i++) {
_threads.emplace_back([&cook = _cooks[i]]() {
cook.nextOrder();
});
}

Related

std::unique_ptr use of deleted function with move

there is the following code:
struct RenderableAtmosphere {
std::unique_ptr<Atmosphere> atmosphere;
float hScaleFactor, parentEarthSizeCoefficient;
bool isUseToneMapping = false;
};
struct RenderableSceneComponent {
...
std::vector<RenderableAtmosphere> atmospheres;
};
...
RenderableSceneComponent uranusSystemComponent;
uranusSystemComponent.atmospheres = vector<RenderableAtmosphere>{move(renderableUranusAtmosphere)};
//uranusSystemComponent.atmospheres.push_back(move(renderableUranusAtmosphere));
In the case of a move assignment operator call, the code does not compile with the following set of errors:
In file included from D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/vector:62,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Auxiliary_Modules/Camera.h:5,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Auxiliary_Modules/AuxiliaryModules.h:4,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Application.h:3,
from D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Application.cpp:1:
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_construct.h: In instantiation of 'void std::_Construct(_T1*, _Args&& ...) [with _T1 = RenderableAtmosphere; _Args = {const RenderableAtmosphere&}]':
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_uninitialized.h:83:18: required from 'static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const RenderableAtmosphere*; _ForwardIterator = RenderableAtmosphere*; bool _TrivialValueTypes = false]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_uninitialized.h:134:15: required from '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = const RenderableAtmosphere*; _ForwardIterator = RenderableAtmosphere*]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_uninitialized.h:289:37: required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = const RenderableAtmosphere*; _ForwardIterator = RenderableAtmosphere*; _Tp = RenderableAtmosphere]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:1464:33: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const RenderableAtmosphere*; _Tp = RenderableAtmosphere; _Alloc = std::allocator<RenderableAtmosphere>]'
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_vector.h:519:2: required from 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = RenderableAtmosphere; _Alloc = std::allocator<RenderableAtmosphere>; std::vector<_Tp, _Alloc>::allocator_type = std::allocator<RenderableAtmosphere>]'
D:\Clion\OpenGL_Projects\CourseWork_SolarSystem\src\Application.cpp:888:102: required from here
D:/MinGW/mingw32/lib/gcc/i686-w64-mingw32/8.1.0/include/c++/bits/stl_construct.h:75:7: error: use of deleted function 'RenderableAtmosphere::RenderableAtmosphere(const RenderableAtmosphere&)'
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
I cannot figure out at what point a copy of the class is called, which tries to call the deleted copy constructor on unique_ptr. But if you use push_back, then everything works.
The statement
uranusSystemComponent.atmospheres = vector<RenderableAtmosphere>{move(renderableUranusAtmosphere)};
calls the assignment operator of vector:
vector& operator=( const vector& other );
which requires a copy constructor for RenderableAtmosphere with the signature:
RenderableAtmosphere::RenderableAtmosphere(const RenderableAtmosphere&)
But RenderableAtmosphere does not have such a copy constructor since it has a std::unique_ptr member. Copy Constructors on cppreference.com
That's the reason why you get these error messages.

Strange error from move and copy constructors that have shared_ptrs

I'm getting the following errors when trying compile my HuffmanNode class for an assignment, it seems to be something to do with the shared_ptrs in the class, but I have no idea as to what is actually going wrong because of the sheer amount of text that I can't seem to find the actual problem.
g++ -std=c++11 -c src/HuffmanNode.cpp -o obj/HuffmanNode.o -I headers
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/memory:63,
from headers/HuffmanNode.hpp:9,
from src/HuffmanNode.cpp:5:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<FLNRHE001::HuffmanNode>]’
/usr/include/c++/7/bits/shared_ptr_base.h:526:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:637:4: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:1295:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr.h:344:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode]’
/usr/include/c++/7/bits/shared_ptr.h:690:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
/usr/include/c++/7/bits/shared_ptr.h:706:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Args = {const std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
src/HuffmanNode.cpp:34:72: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘FLNRHE001::HuffmanNode::HuffmanNode(const std::shared_ptr<FLNRHE001::HuffmanNode>&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(FLNRHE001::HuffmanNode&&)
HuffmanNode::HuffmanNode(HuffmanNode &&n)
^~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: no known conversion for argument 1 from ‘const std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘FLNRHE001::HuffmanNode&&’
src/HuffmanNode.cpp:33:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(const FLNRHE001::HuffmanNode&)
HuffmanNode::HuffmanNode(const HuffmanNode &n)
^~~~~~~~~~~
src/HuffmanNode.cpp:33:1: note: no known conversion for argument 1 from ‘const std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘const FLNRHE001::HuffmanNode&’
src/HuffmanNode.cpp:11:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(char, int, std::shared_ptr<FLNRHE001::HuffmanNode>, std::shared_ptr<FLNRHE001::HuffmanNode>)
HuffmanNode::HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
^~~~~~~~~~~
src/HuffmanNode.cpp:11:1: note: candidate expects 4 arguments, 1 provided
In file included from /usr/include/x86_64-linux-gnu/c++/7/bits/c++allocator.h:33:0,
from /usr/include/c++/7/bits/allocator.h:46,
from /usr/include/c++/7/memory:63,
from headers/HuffmanNode.hpp:9,
from src/HuffmanNode.cpp:5:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp
= FLNRHE001::HuffmanNode]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_Tp1> >::construct(std::allocator_traits<std::allocator<_Tp1> >::allocator_type&, _Up*, _Args&& ...) [with _Up = FLNRHE001::HuffmanNode; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; std::allocator_traits<std::allocator<_Tp1> >::allocator_type = std::allocator<FLNRHE001::HuffmanNode>]’
/usr/include/c++/7/bits/shared_ptr_base.h:526:39: required from ‘std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&};
_Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:637:4: required from ‘std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr_base.h:1295:35: required from ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2]’
/usr/include/c++/7/bits/shared_ptr.h:344:64: required from ‘std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}; _Tp = FLNRHE001::HuffmanNode]’
/usr/include/c++/7/bits/shared_ptr.h:690:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Alloc = std::allocator<FLNRHE001::HuffmanNode>; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
/usr/include/c++/7/bits/shared_ptr.h:706:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = FLNRHE001::HuffmanNode; _Args = {std::shared_ptr<FLNRHE001::HuffmanNode>&}]’
src/HuffmanNode.cpp:93:52: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘FLNRHE001::HuffmanNode::HuffmanNode(std::shared_ptr<FLNRHE001::HuffmanNode>&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(FLNRHE001::HuffmanNode&&)
HuffmanNode::HuffmanNode(HuffmanNode &&n)
^~~~~~~~~~~
src/HuffmanNode.cpp:41:1: note: no known conversion for argument 1 from ‘std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘FLNRHE001::HuffmanNode&&’
src/HuffmanNode.cpp:33:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(const FLNRHE001::HuffmanNode&)
HuffmanNode::HuffmanNode(const HuffmanNode &n)
^~~~~~~~~~~
src/HuffmanNode.cpp:33:1: note: no known conversion for argument 1 from ‘std::shared_ptr<FLNRHE001::HuffmanNode>’ to ‘const FLNRHE001::HuffmanNode&’
src/HuffmanNode.cpp:11:1: note: candidate: FLNRHE001::HuffmanNode::HuffmanNode(char, int, std::shared_ptr<FLNRHE001::HuffmanNode>, std::shared_ptr<FLNRHE001::HuffmanNode>)
HuffmanNode::HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
^~~~~~~~~~~
src/HuffmanNode.cpp:11:1: note: candidate expects 4 arguments, 1 provided
I've tried looking up the errors but can't seem to find anything that solves the problem.
I can't post my entire code due to it being an assignment, but the lines that are mentioned in the error text are my constructor declarations, and as stated above, the problem seems to lie in the shared_ptrs:
line 11: HuffmanNode::HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
line 33: HuffmanNode::HuffmanNode(const HuffmanNode &n) : ch(n.ch), freq(n.freq), left(std::make_shared<HuffmanNode>(n.left)), right(std::make_shared<HuffmanNode>(n.right))
line 41: HuffmanNode::HuffmanNode(HuffmanNode &&n) : ch(std::move(n.ch)), freq(std::move(n.freq)), left(std::move(n.left)), right(std::move(n.right))
EDIT:
Here's what I can post according to the school regulations, the only method that has any content inside it of the problem areas is the move constructor, and that is just ensuring that there will be no double deletion errors when the destructor is called. This gives me the same errors as above does.
#include <memory>
class HuffmanNode
{
char ch;
int freq;
std::shared_ptr<HuffmanNode> left, right;
// Node constructor for leaf node
HuffmanNode(char c, int f, std::shared_ptr<HuffmanNode> l, std::shared_ptr<HuffmanNode> r)
: ch(c), freq(f), left(l), right(r)
{
// Handled in initialiser list
}
HuffmanNode(const HuffmanNode &n)
: ch(n.ch), freq(n.freq), left(std::make_shared<HuffmanNode>(n.left)),
right(std::make_shared<HuffmanNode>(n.right))
{
// Handled by initialiser list
}
// Move Constructor
HuffmanNode(HuffmanNode &&n)
: ch(std::move(n.ch)), freq(std::move(n.freq)),
left(std::move(n.left)),
right(std::move(n.right))
{
}
};
The errors were arising from attempting to use make_shared to create a copy of the left and right pointers, in this case I only require a shallow copy as is made by the shared_ptr copy constructor.
The corrected solution was only a change to the move constructor as follows-:
HuffmanNode(const HuffmanNode &n)
: ch(n.ch), freq(n.freq), left(n.left),
right(n.right)
{
// Handled by initialiser list
}
Thanks to Sam Varshavchik for the help in this!

Creating and storing string in a union inside a struct in a vector C++14 (Unrestricted Union)

Can anyone please explain how to assign and push a string(in a union inside a struct) into a vector? Is this possible? Is the vector trying to access invalid memory?
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef struct {
int height;
int width;
} Page;
typedef struct varstruct{
int test;
union uni{
Page page;
int intVar;
string stringVar;
uni(){
new (&stringVar) std::string();
}
~uni(){}
} VarUnion;
varstruct(){}
~varstruct(){}
} VariableDataStruct;
vector<VariableDataStruct> List;
int main()
{
VariableDataStruct structeg;
structeg.test = 1;
structeg.VarUnion.stringVar = "Test";
List.push_back(structeg);
cout<<structeg.VarUnion.stringVar<<endl;
structeg.VarUnion.stringVar.~basic_string();
return 0;
}
The code works fine in the absence "List.push_back(structeg);" statement. On addition of this statement gives the following errors:
In file included from /usr/include/c++/4.8.3/x86_64-redhat-linux/bits/c++allocator.h:33:0,
from /usr/include/c++/4.8.3/bits/allocator.h:46,
from /usr/include/c++/4.8.3/string:41,
from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
from /usr/include/c++/4.8.3/bits/ios_base.h:41,
from /usr/include/c++/4.8.3/ios:42,
from /usr/include/c++/4.8.3/ostream:38,
from /usr/include/c++/4.8.3/iostream:39,
from unionstring2.cc:1:
/usr/include/c++/4.8.3/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = varstruct; _Args = {const varstruct&}; _Tp = varstruct]’:
/usr/include/c++/4.8.3/bits/alloc_traits.h:254:4: required from ‘static typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = varstruct; _Args = {const varstruct&}; _Alloc = std::allocator<varstruct>; typename std::enable_if<std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::value, void>::type = void]’
/usr/include/c++/4.8.3/bits/alloc_traits.h:393:57: required from ‘static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = varstruct; _Args = {const varstruct&}; _Alloc = std::allocator<varstruct>; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]’
/usr/include/c++/4.8.3/bits/stl_vector.h:906:34: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = varstruct; _Alloc = std::allocator<varstruct>; std::vector<_Tp, _Alloc>::value_type = varstruct]’
unionstring2.cc:34:28: required from here
/usr/include/c++/4.8.3/ext/new_allocator.h:120:4: error: use of deleted function ‘varstruct::varstruct(const varstruct&)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^
unionstring2.cc:12:16: note: ‘varstruct::varstruct(const varstruct&)’ is implicitly deleted because the default definition would be ill-formed:
typedef struct varstruct{
^
unionstring2.cc:12:16: error: use of deleted function ‘varstruct::uni::uni(const varstruct::uni&)’
unionstring2.cc:14:19: note: ‘varstruct::uni::uni(const varstruct::uni&)’ is implicitly deleted because the default definition would be ill-formed:
union uni{
^
unionstring2.cc:17:10: error: union member ‘varstruct::uni::stringVar’ with non-trivial ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
string stringVar;
^
In file included from /usr/include/c++/4.8.3/vector:62:0,
from unionstring2.cc:3:
/usr/include/c++/4.8.3/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = varstruct; _Args = {varstruct}]’:
/usr/include/c++/4.8.3/bits/stl_uninitialized.h:75:53: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<varstruct*>; _ForwardIterator = varstruct*; bool _TrivialValueTypes = false]’
/usr/include/c++/4.8.3/bits/stl_uninitialized.h:117:41: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<varstruct*>; _ForwardIterator = varstruct*]’
/usr/include/c++/4.8.3/bits/stl_uninitialized.h:258:63: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<varstruct*>; _ForwardIterator = varstruct*; _Tp = varstruct]’
/usr/include/c++/4.8.3/bits/stl_uninitialized.h:281:69: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = varstruct*; _ForwardIterator = varstruct*; _Allocator = std::allocator<varstruct>]’
/usr/include/c++/4.8.3/bits/vector.tcc:415:43: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {const varstruct&}; _Tp = varstruct; _Alloc = std::allocator<varstruct>]’
/usr/include/c++/4.8.3/bits/stl_vector.h:911:27: required from ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = varstruct; _Alloc = std::allocator<varstruct>; std::vector<_Tp, _Alloc>::value_type = varstruct]’
unionstring2.cc:34:28: required from here
/usr/include/c++/4.8.3/bits/stl_construct.h:75:7: error: use of deleted function ‘varstruct::varstruct(const varstruct&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
Is there any alternative?
One of members of your union is string, according to reference
If a union contains a non-static data member with a non-trivial
special member function (copy/move constructor, copy/move assignment,
or destructor), that function is deleted by default in the union and
needs to be defined explicitly by the programmer.
so you cannot compile because during the call of vector::push_back copy constructor of uni is called, but this function is deleted. If you want to compile your code you need to add copy constructor:
uni(const uni& u) {
new (&stringVar) std::string{u.stringVar};
}
then your code compiles and even works, but it is not good approach to use union with non-trivial types. You should add type member to your struct to control which type of your union is active. Then you can delete in destructor the right member (call destructor for non-trivial types). Good example is presented in Bjarne Stroustrup book C++ Programming Language , chapter Anonymous Unions.
Modified version (it is not full code, only IDEA) of your code with type member in struct:
enum TYPE {INT,STR,PAGE};
typedef struct varstruct{
int test;
union {
Page page;
int intVar;
string stringVar;
};
void setPage (Page p) {
if (type == STR)
stringVar.~string();
page = p;
type = PAGE;
}
void setInt (int i) {
if (type == STR)
stringVar.~string();
intVar = i;
type = INT;
}
void setString (string s) {
if (type == STR)
stringVar = s;
else
new (&stringVar) std::string{s};
type = STR;
}
TYPE type = INT;
varstruct (const varstruct& v) {
if (v.type == STR)
new (&stringVar) std::string{v.stringVar};
else if (v.type == INT)
intVar = v.intVar;
else if (v.type == PAGE)
page = v.page;
type = v.type;
}
varstruct(){}
~varstruct(){
if (type == STR)
stringVar.~string();
}
} VariableDataStruct;
and the code in main:
VariableDataStruct structeg;
structeg.test = 1;
structeg.setString("Test");
List.push_back(structeg);
cout<<structeg.stringVar<<endl;

Create vector of mocked class in google test

Is it possible to build a vector of mocked objects in Google Test/Mock? I have a scenario, where I want to return a vector of custom objects (Foo) from an object (Bar). So I was trying return vector with mocked version of that class (std::vector<Foo>).
Example code:
class Foo {
public:
virtual int op(int a , int b) {
return 0;
}
};
class Bar {
public:
virtual std::vector<Foo> getFoos() {
std::vector<Foo> v;
// ...
// Some logic to fill this vector
// ...
return v;
}
};
class MockFoo : public Foo {
public:
MOCK_METHOD2(op, int(int, int));
};
class MockBar : public Bar {
public:
MOCK_METHOD0(getFoos, std::vector<Foo>());
};
TEST(Foo, test_op) {
vector<MockFoo> v;
v.emplace_back();
ASSERT_EQ(v.size(), 1);
MockBar bar;
EXPECT_CALL(bar, getFoos())
.WillRepeatedly(Return(v));
}
I am getting
In file included from .../googlemock/include/gmock/gmock.h:58:0,
from .../FooBarTest.cpp:2:
.../googlemock/include/gmock/gmock-actions.h: In instantiation of ‘testing::internal::ReturnAction<R>::Impl<R_, F>::Impl(const testing::internal::linked_ptr<T>&) [with R_ = std::vector<MockFoo>; F = std::vector<Foo>(); R = std::vector<MockFoo>]’:
.../googlemock/include/gmock/gmock-actions.h:557:44: required from ‘testing::internal::ReturnAction<R>::operator testing::Action<Func>() const [with F = std::vector<Foo>(); R = std::vector<MockFoo>]’
.../FooBarTest.cpp:65:38: required from here
.../googlemock/include/gmock/gmock-actions.h:577:39: error: no matching function for call to ‘ImplicitCast_(std::vector<MockFoo>&)’
value_(ImplicitCast_<Result>(value_before_cast_)) {}
^
In file included from .../googletest/include/gtest/internal/gtest-internal.h:40:0,
from .../googletest/include/gtest/gtest.h:58,
from .../FooBarTest.cpp:1:
.../googletest/include/gtest/internal/gtest-port.h:1343:11: note: candidate: template<class To> To testing::internal::ImplicitCast_(To)
inline To ImplicitCast_(To x) { return x; }
^
.../googletest/include/gtest/internal/gtest-port.h:1343:11: note: template argument deduction/substitution failed:
In file included from .../googlemock/include/gmock/gmock.h:58:0,
from .../FooBarTest.cpp:2:
.../googlemock/include/gmock/gmock-actions.h:577:39: note: cannot convert ‘((testing::internal::ReturnAction<std::vector<MockFoo> >::Impl<std::vector<MockFoo>, std::vector<Foo>()>*)this)->testing::internal::ReturnAction<std::vector<MockFoo> >::Impl<std::vector<MockFoo>, std::vector<Foo>()>::value_before_cast_’ (type ‘std::vector<MockFoo>’) to type ‘std::vector<Foo>’
value_(ImplicitCast_<Result>(value_before_cast_)) {}
^
In file included from /usr/include/c++/5/vector:62:0,
from .../googletest/include/gtest/gtest.h:56,
from .../FooBarTest.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = MockFoo; _Args = {const MockFoo&}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const MockFoo*, std::vector<MockFoo> >; _ForwardIterator = MockFoo*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const MockFoo*, std::vector<MockFoo> >; _ForwardIterator = MockFoo*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const MockFoo*, std::vector<MockFoo> >; _ForwardIterator = MockFoo*; _Tp = MockFoo]’
/usr/include/c++/5/bits/stl_vector.h:322:31: required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = MockFoo; _Alloc = std::allocator<MockFoo>]’
.../FooBarTest.cpp:65:37: required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: use of deleted function ‘MockFoo::MockFoo(const MockFoo&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
.../FooBarTest.cpp:40:7: note: ‘MockFoo::MockFoo(const MockFoo&)’ is implicitly deleted because the default definition would be ill-formed:
class MockFoo : public Foo {
^
.../FooBarTest.cpp:40:7: error: use of deleted function ‘testing::internal::FunctionMocker<int(int, int)>::FunctionMocker(const testing::internal::FunctionMocker<int(int, int)>&)’
In file included from .../googlemock/include/gmock/gmock.h:61:0,
from .../FooBarTest.cpp:2:
.../googlemock/include/gmock/gmock-generated-function-mockers.h:106:7: note: ‘testing::internal::FunctionMocker<int(int, int)>::FunctionMocker(const testing::internal::FunctionMocker<int(int, int)>&)’ is implicitly deleted because the default definition would be ill-formed:
class FunctionMocker<R(A1, A2)> : public
^
In file included from .../googletest/include/gtest/internal/gtest-internal.h:40:0,
from .../googletest/include/gtest/gtest.h:58,
from .../FooBarTest.cpp:1:
.../googlemock/include/gmock/gmock-spec-builders.h:1784:35: error: ‘testing::internal::FunctionMockerBase<F>::FunctionMockerBase(const testing::internal::FunctionMockerBase<F>&) [with F = int(int, int)]’ is private
GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
^
.../googletest/include/gtest/internal/gtest-port.h:875:3: note: in definition of macro ‘GTEST_DISALLOW_COPY_AND_ASSIGN_’
type(type const &);\
^
In file included from .../googlemock/include/gmock/gmock.h:61:0,
from .../FooBarTest.cpp:2:
.../googlemock/include/gmock/gmock-generated-function-mockers.h:106:7: error: within this context
class FunctionMocker<R(A1, A2)> : public
^
In file included from /usr/include/c++/5/vector:62:0,
from .../googletest/include/gtest/gtest.h:56,
from .../FooBarTest.cpp:1:
/usr/include/c++/5/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = MockFoo; _Args = {MockFoo}]’:
/usr/include/c++/5/bits/stl_uninitialized.h:75:18: required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<MockFoo*>; _ForwardIterator = MockFoo*; bool _TrivialValueTypes = false]’
/usr/include/c++/5/bits/stl_uninitialized.h:126:15: required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::move_iterator<MockFoo*>; _ForwardIterator = MockFoo*]’
/usr/include/c++/5/bits/stl_uninitialized.h:281:37: required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::move_iterator<MockFoo*>; _ForwardIterator = MockFoo*; _Tp = MockFoo]’
/usr/include/c++/5/bits/stl_uninitialized.h:303:2: required from ‘_ForwardIterator std::__uninitialized_move_if_noexcept_a(_InputIterator, _InputIterator, _ForwardIterator, _Allocator&) [with _InputIterator = MockFoo*; _ForwardIterator = MockFoo*; _Allocator = std::allocator<MockFoo>]’
/usr/include/c++/5/bits/vector.tcc:422:8: required from ‘void std::vector<_Tp, _Alloc>::_M_emplace_back_aux(_Args&& ...) [with _Args = {}; _Tp = MockFoo; _Alloc = std::allocator<MockFoo>]’
/usr/include/c++/5/bits/vector.tcc:101:23: required from ‘void std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = MockFoo; _Alloc = std::allocator<MockFoo>]’
.../FooBarTest.cpp:56:20: required from here
/usr/include/c++/5/bits/stl_construct.h:75:7: error: use of deleted function ‘MockFoo::MockFoo(MockFoo&&)’
{ ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
^
.../FooBarTest.cpp:40:7: note: ‘MockFoo::MockFoo(MockFoo&&)’ is implicitly deleted because the default definition would be ill-formed:
class MockFoo : public Foo {
^
.../FooBarTest.cpp:40:7: error: use of deleted function ‘testing::internal::FunctionMocker<int(int, int)>::FunctionMocker(testing::internal::FunctionMocker<int(int, int)>&&)’
In file included from .../googlemock/include/gmock/gmock.h:61:0,
from .../FooBarTest.cpp:2:
.../googlemock/include/gmock/gmock-generated-function-mockers.h:106:7: note: ‘testing::internal::FunctionMocker<int(int, int)>::FunctionMocker(testing::internal::FunctionMocker<int(int, int)>&&)’ is implicitly deleted because the default definition would be ill-formed:
class FunctionMocker<R(A1, A2)> : public
^
In file included from .../googletest/include/gtest/internal/gtest-internal.h:40:0,
from .../googletest/include/gtest/gtest.h:58,
from .../FooBarTest.cpp:1:
.../googlemock/include/gmock/gmock-spec-builders.h:1784:35: error: ‘testing::internal::FunctionMockerBase<F>::FunctionMockerBase(const testing::internal::FunctionMockerBase<F>&) [with F = int(int, int)]’ is private
GTEST_DISALLOW_COPY_AND_ASSIGN_(FunctionMockerBase);
^
.../googletest/include/gtest/internal/gtest-port.h:875:3: note: in definition of macro ‘GTEST_DISALLOW_COPY_AND_ASSIGN_’
type(type const &);\
^
In file included from .../googlemock/include/gmock/gmock.h:61:0,
from .../FooBarTest.cpp:2:
.../googlemock/include/gmock/gmock-generated-function-mockers.h:106:7: error: within this context
class FunctionMocker<R(A1, A2)> : public
To my best understanding it is due to the fact Google test mock classes are not copyable. I explicitly don't need a copyable mock class. I just need the ability to return list of mocked object. Is it possible in Google test (release-1.8.0)?
One alternative that comes to mind is implement an iterator like patter in Bar, so that it behaves like a vector. However, I want to avoid that approach if possible.
You need to implement a copy constructor inside your Mockclass, that's how I managed to create a mock class vector. Probably something like this:
MockClass(const MockClass& other) {
setDefaultBehaviors();
defaultId = other.defaultId;
}

can't push my object to std::vector - gcc can't find something like copy-construct

I'm using such field:
std::vector<OneItemIndex> oneItemIndexes;
This is part of OneItemIndex declaration:
class OneItemIndex : public CustomIndex
{
public:
OneItemIndex(int instrumentId_);
~OneItemIndex(void);
OneItemIndex(OneItemIndex& rhs);
...
In VC++ this compiles just fine, but in gcc I receive such compilation error:
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’
Why gcc asks for OneItemIndex::OneItemIndex(OneItemIndex)? Should I add such construction, how to implement it?
Change
OneItemIndex(OneItemIndex& rhs);
to
OneItemIndex(OneItemIndex const& rhs);
in the class definition.