How to create std::thread with function pointer with a reference? - c++

class taskq {
public:
int trigger(taskq &tq);
mutex mtx;
};
void func_wrapper(taskq &tq) {
cout<<endl;
}
int taskq::trigger(taskq &tq) {
thread thread(func_wrapper, tq);
return 0;
}
I tried to compile the above simple code but I am keep getting errors because of line thread thread(func_wrapper, tq);.
What's wrong with this code?
How could I fix it?
The error message is as below:
In file included from /usr/include/c++/4.9/thread:39:0,
from taskq.C:2:
/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(taskq))(taskq&)>’:
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33: required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(taskq))(taskq&)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(taskq))(taskq&)>’
_M_invoke(_Index_tuple<_Indices...>)
^
In file included from /usr/include/c++/4.9/functional:55:0,
from /usr/include/c++/4.9/thread:39,
from taskq.C:2:
/usr/include/c++/4.9/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = taskq; <template-parameter-2-2> = void; long unsigned int _Idx = 1ul; _Head = taskq]’:
/usr/include/c++/4.9/tuple:271:42: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 1ul; _Head = taskq; _Tail = {}]’
/usr/include/c++/4.9/type_traits:900:43: required by substitution of ‘template<class _Tp, class _Arg, class> static std::true_type std::__do_is_direct_constructible_impl::__test(int) [with _Tp = std::_Tuple_impl<1ul, taskq>; _Arg = std::_Tuple_impl<1ul, taskq>&&; <template-parameter-1-3> = <missing>]’
/usr/include/c++/4.9/type_traits:912:43: required from ‘struct std::__is_direct_constructible_impl<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&>’
/usr/include/c++/4.9/type_traits:134:12: required from ‘struct std::__and_<std::is_destructible<std::_Tuple_impl<1ul, taskq> >, std::__is_direct_constructible_impl<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&> >’
/usr/include/c++/4.9/type_traits:916:12: required from ‘struct std::__is_direct_constructible_new_safe<std::_Tuple_impl<1ul, taskq>, std::_Tuple_impl<1ul, taskq>&&>’
/usr/include/c++/4.9/type_traits:994:12: [ skipping 7 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/4.9/type_traits:1175:12: required from ‘struct std::is_nothrow_move_constructible<std::_Tuple_impl<1ul, taskq> >’
/usr/include/c++/4.9/type_traits:134:12: required from ‘struct std::__and_<std::is_nothrow_move_constructible<void (*)(taskq&)>, std::is_nothrow_move_constructible<std::_Tuple_impl<1ul, taskq> > >’
/usr/include/c++/4.9/tuple:267:7: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0ul; _Head = void (*)(taskq&); _Tail = {taskq}]’
/usr/include/c++/4.9/functional:1727:41: required from ‘typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}; typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type = std::_Bind_simple<void (*(taskq))(taskq&)>]’
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33: required from here
/usr/include/c++/4.9/tuple:140:42: error: use of deleted function ‘taskq::taskq(taskq&&)’
: _M_head_impl(std::forward<_UHead>(__h)) { }
^
taskq.C:9:7: note: ‘taskq::taskq(taskq&&)’ is implicitly deleted because the default definition would be ill-formed:
class taskq {
^
taskq.C:9:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from taskq.C:3:0:
/usr/include/c++/4.9/mutex:129:5: note: declared here
mutex(const mutex&) = delete;
^
In file included from /usr/include/c++/4.9/functional:55:0,
from /usr/include/c++/4.9/thread:39,
from taskq.C:2:
/usr/include/c++/4.9/tuple: In instantiation of ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long unsigned int _Idx = 1ul; _Head = taskq]’:
/usr/include/c++/4.9/tuple:255:44: recursively required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 1ul; _Head = taskq; _Tail = {}]’
/usr/include/c++/4.9/tuple:255:44: required from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul; _Head = void (*)(taskq&); _Tail = {taskq}]’
/usr/include/c++/4.9/tuple:531:30: required from ‘constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = void (*)(taskq&); _T2 = taskq]’
/usr/include/c++/4.9/functional:1678:74: required from ‘std::_Bind_simple<_Callable(_Args ...)>::_Bind_simple(_Callable&&, _Args2&& ...) [with _Args2 = {taskq&}; <template-parameter-2-2> = void; _Callable = void (*)(taskq&); _Args = {taskq}]’
/usr/include/c++/4.9/functional:1727:41: required from ‘typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type std::__bind_simple(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}; typename std::_Bind_simple_helper<_Func, _BoundArgs>::__type = std::_Bind_simple<void (*(taskq))(taskq&)>]’
/usr/include/c++/4.9/thread:140:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(taskq&); _Args = {taskq&}]’
taskq.C:20:33: required from here
/usr/include/c++/4.9/tuple:134:25: error: use of deleted function ‘taskq::taskq(const taskq&)’
: _M_head_impl(__h) { }
^
taskq.C:9:7: note: ‘taskq::taskq(const taskq&)’ is implicitly deleted because the default definition would be ill-formed:
class taskq {
^
taskq.C:9:7: error: use of deleted function ‘std::mutex::mutex(const std::mutex&)’
In file included from taskq.C:3:0:
/usr/include/c++/4.9/mutex:129:5: note: declared here
mutex(const mutex&) = delete;
^
make: *** [taskq.o] Error 1

thread thread(func_wrapper, std::ref(tq));
Note that you need to make sure the reference is still valid while you are executing func_wrapper (since I'm guessing such function uses tq).

std::thread thread([&tq]() {
func_wrapper(tq);
});

Related

Using std::allocate_shared with polymorphic resource allocators

I am attempting to create shared pointers with a std::pmr::monotonic_buffer_resource, and I cannot make it compile. What am I missing?
https://godbolt.org/z/R9jdju
#include <memory>
#include <memory_resource>
int main() {
char buffer[100];
std::pmr::monotonic_buffer_resource mbr(buffer, 100);
std::shared_ptr<double> sp = std::allocate_shared<double>(mbr);
}
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/ext/alloc_traits.h:34,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/stl_uninitialized.h:67,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:66,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/alloc_traits.h: In substitution of 'template<class _Alloc, class _Up> using __alloc_rebind = typename std::__allocator_traits_base::__rebind<_Alloc, _Up>::type [with _Alloc = std::pmr::monotonic_buffer_resource; _Up = std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>]':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:542:13: required from 'class std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:679:43: required from 'std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:1371:71: required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:408:59: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:859:14: required from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}]'
<source>:7:66: required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/alloc_traits.h:78:11: error: no type named 'type' in 'struct std::__allocator_traits_base::__rebind<std::pmr::monotonic_buffer_resource, std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>, void>'
78 | using __alloc_rebind
| ^~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:52,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:84,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h: In instantiation of 'std::__shared_count<_Lp>::__shared_count(_Tp*&, std::_Sp_alloc_shared_tag<_Alloc>, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]':
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:1371:71: required from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:408:59: required from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_alloc_shared_tag<_Tp>, _Args&& ...) [with _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}; _Tp = double]'
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:859:14: required from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; _Args = {}]'
<source>:7:66: required from here
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:682:16: error: use of deleted function 'std::pmr::monotonic_buffer_resource::monotonic_buffer_resource(const std::pmr::monotonic_buffer_resource&)'
682 | auto __pi = ::new (__mem)
| ^~~~~~~~~~~~~
683 | _Sp_cp_type(__a._M_a, std::forward<_Args>(__args)...);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from <source>:2:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory_resource:604:5: note: declared here
604 | monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr.h:52,
from /opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/memory:84,
from <source>:1:
/opt/compiler-explorer/gcc-10.1.0/include/c++/10.1.0/bits/shared_ptr_base.h:546:33: note: initializing argument 1 of 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {}; _Tp = double; _Alloc = std::pmr::monotonic_buffer_resource; __gnu_cxx::_Lock_policy _Lp = __gnu_cxx::_S_atomic]'
546 | _Sp_counted_ptr_inplace(_Alloc __a, _Args&&... __args)
| ~~~~~~~^~~
That long error message basically boils down to these 2 errors:
error: no type named 'type' in 'struct std::__allocator_traits_base::__rebind<std::pmr::monotonic_buffer_resource, std::_Sp_counted_ptr_inplace<double, std::pmr::monotonic_buffer_resource, __gnu_cxx::_S_atomic>, void>'
error: use of deleted function 'std::pmr::monotonic_buffer_resource::monotonic_buffer_resource(const std::pmr::monotonic_buffer_resource&)'
std::pmr::monotonic_buffer_resource does not satisfy the requirements that std::allocate_shared() expects, specifically:
All memory allocation is done using a copy of alloc, which must satisfy the Allocator requirements.
In particular, "a COPY of alloc", which fails since monotonic_buffer_resource's copy constructor is delete'd so it can't be copied.
As #MilesBudnek stated in comments, you can wrap the monotonic_buffer_resource inside a std::pmr::polymorphic_allocator, which is designed to be used as an Allocator for standard containers:
The class template std::pmr::polymorphic_allocator is an Allocator which exhibits different allocation behavior depending upon the std::pmr::memory_resource from which it is constructed.
For example:
#include <memory>
#include <memory_resource>
int main() {
char buffer[100];
std::pmr::monotonic_buffer_resource mbr(buffer, 100);
auto sp = std::allocate_shared<double, std::pmr::polymorphic_allocator<double>>(&mbr);
}
https://godbolt.org/z/-xFfFY
Alternatively:
#include <memory>
#include <memory_resource>
int main() {
char buffer[100];
std::pmr::monotonic_buffer_resource mbr(buffer, 100);
std::pmr::polymorphic_allocator<double> alloc(&mbr);
auto sp = std::allocate_shared<double>(alloc);
}
https://godbolt.org/z/GLE4-5

Derive from shared_ptr<T>

I have a strange compilation problem that I can't understand.
//I know, you should never derive from the STL Library
template<class T>
class SharedClass : private shared_ptr<T>
{
public:
template<class T2>
SharedClass(T2&& t2) :
shared_ptr<T>(make_shared<T>(move(t2)))
{}
virtual ~SharedClass()
{}
};
class A
{
public:
typedef function<void(SharedClass<A>)> Callback;
A(Callback callback)
{ }
};
main.cpp
SharedClass<A> shared([](SharedClass<A>){ });
Compile Log:
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/string:41,
from /usr/include/c++/7/bits/locale_classes.h:40,
from /usr/include/c++/7/bits/ios_base.h:41,
from /usr/include/c++/7/ios:42,
from /usr/include/c++/7/ostream:38,
from /usr/include/c++/7/iostream:39,
from main.cpp:9:
/usr/include/c++/7/ext/new_allocator.h: In instantiation of ‘void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = A; _Args = {SharedClass<A>}; _Tp = A]’:
/usr/include/c++/7/bits/alloc_traits.h:475:4: required from ‘static void std::allocator_traits<std::allocator<_CharT>>::construct(std::allocator_traits<std::allocator<_CharT> >::allocator_type&, _Up*, _Args&& ...) [with _Up = A; _Args = {SharedClass<A>}; _Tp = A; std::allocator_traits<std::allocator<_CharT>>::allocator_type = std::allocator<A>]’
/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 = {SharedClass<A>}; _Tp = A; _Alloc = std::allocator<A>; __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 = A; _Alloc = std::allocator<A>; _Args = {SharedClass<A>}; __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<A>; _Args = {SharedClass<A>}; _Tp = A; __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<A>; _Args = {SharedClass<A>}; _Tp = A]’
/usr/include/c++/7/bits/shared_ptr.h:690:14: [ skipping 9 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/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<A>; _Args = {main()::<lambda(SharedClass<A>)>}; _Tp = A; __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<A>; _Args = {main()::<lambda(SharedClass<A>)>}; _Tp = A]’ /usr/include/c++/7/bits/shared_ptr.h:690:14: required from ‘std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = A; _Alloc = std::allocator<A>; _Args = {main()::<lambda(SharedClass<A>)>}]’
/usr/include/c++/7/bits/shared_ptr.h:706:39: required from ‘std::shared_ptr<_Tp> std::make_shared(_Args&& ...) [with _Tp = A; _Args = {main()::<lambda(SharedClass<A>)>}]’
main.cpp:21:33: required from ‘SharedClass<T>::SharedClass(T2&&) [with T2 = main()::<lambda(SharedClass<A>)>; T = A]’
main.cpp:39:48: required from here
/usr/include/c++/7/ext/new_allocator.h:136:4: error: no matching function for call to ‘A::A(SharedClass)’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:33:5: note: candidate: A::A(A::Callback)
A(Callback callback)
^ main.cpp:33:5: note: no known conversion for argument 1 from ‘SharedClass’ to ‘A::Callback {aka std::function)>}’
main.cpp:28:7: note: candidate: constexpr A::A(const A&)
class A
^ main.cpp:28:7: note: no known conversion for argument 1 from ‘SharedClass’ to ‘const A&’
main.cpp:28:7: note: candidate: constexpr A::A(A&&)
main.cpp:28:7: note: no known conversion for argument 1 from ‘SharedClass’ to ‘A&&’
If I change the constructor of the SharedClass class like this SharedClass (T2 t2) it compiles.
As your lambda takes its parameters by value it needs to be able to create copies of those parameters, it does this by calling the move constructor. As you have defined a templated constructor with the same signature as the move constructor the compiler attempts to use it as a move constructor. Adding default copy (for completeness) and move constructors fixes the issue:
SharedClass(const SharedClass&) = default;
SharedClass(SharedClass&&) = default;
Your existing constructor doesn't work as a move constructor because there is no constructor for A which takes a SharedClass<A> parameter so make_shared<A> doesn't compile when passed a SharedClass<A> parameter.

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!

GMOCK method is throwing compilation error

When I try to implementation GMOCK method like below I get compilation error but the code and the MOCK class works fine when I remove it:
MOCK_METHOD2(myfunc, void (std::shared_ptr<route>, std::unique_ptr<message, std::default_delete<message> >));
I get the below compilation error:
error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = message; _Dp = std::default_delete<message>]'
MOCK_METHOD2(ship, void (std::shared_ptr<route>, std::unique_ptr<message, std::default_delete<message> >));
^
In file included from C:/tools/mingw64/x86_64-w64-mingw32/include/c++/memory:81:0,
......................................................................................
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/bits/unique_ptr.h:356:7: note: declared here
unique_ptr(const unique_ptr&) = delete;
error: initializing argument 2 of 'R testing::internal::FunctionMocker<R(A1, A2)>::Invoke(A1, A2) [with R = void; A1 = std::shared_ptr<route>; A2 = std::unique_ptr<message>]'
R Invoke(A1 a1, A2 a2) {
^
In file included from ...
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple: In instantiation of 'constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long long unsigned int _Idx = 1ull; _Head = std::unique_ptr<message>]':
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:255:44: recursively required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long long unsigned int _Idx = 1ull; _Head = std::unique_ptr<message>; _Tail = {}]'
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:255:44: required from 'constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long long unsigned int _Idx = 0ull; _Head = std::shared_ptr<cmb::mim::MimChannel>; _Tail = {std::unique_ptr<message, std::default_delete<message> >}]'
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:531:30: required from 'constexpr std::tuple<_T1, _T2>::tuple(const _T1&, const _T2&) [with _T1 = std::shared_ptr<cmb::mim::MimChannel>; _T2 = std::unique_ptr<message>]'
C:/work/googletest/googlemock/include/gmock/gmock-generated-function-mockers.h:122:50: required from 'R testing::internal::FunctionMocker<R(A1, A2)>::Invoke(A1, A2) [with R = void; A1 = std::shared_ptr<cmb::mim::MimChannel>; A2 = std::unique_ptr<message>]'
C:/work/Mock.hpp:39:5: required from here
C:/tools/mingw64/x86_64-w64-mingw32/include/c++/tuple:134:25: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = message; _Dp = std::default_delete<message>]'
: _M_head_impl(__h) { }
I am not sure what the error is and how to fix it - commenting or removing the MOCK method declaration the compilation goes fine?
I was able to resolve the issue by manipulating the function as mentioned below:
MOCK_METHOD2(myfuncproxy, void (std::shared_ptr<route>, message&));
void myfunc((std::shared_ptr<route> r, std::unique_ptr<message, std::default_delete<message> > m)
{
myfuncproxy(r,*m.get());
}

Passing an object by reference in C++

Before people start telling me to do a google search for my problem, let me say I've been trying for quite a while.
I can't figure out how to pass an object by reference in C++, I keep getting a massive printout of compiler errors. I can include them if they would help. Specifically, I'm trying to pass the RSSFeed object feed by reference to the function parseFeed in a thread.
void parseFeed(RSSFeed& feed) {
//dostuff
}
RSSFeed feed();
thread(parseFeed, feed); // line 119
errors:
g++ -g -Wall -pedantic -O0 -std=c++0x -D_GLIBCXX_USE_NANOSLEEP -D_GLIBCXX_USE_SCHED_YIELD -I/usr/class/cs110/include/libxml2 -I/usr/class/cs110/local/include -c -o news-aggregator.o news-aggregator.cc
In file included from /usr/include/c++/4.6/functional:56:0,
from /usr/include/c++/4.6/bits/stl_algo.h:68,
from /usr/include/c++/4.6/algorithm:63,
from news-aggregator.cc:14:
rss-feed.h: In constructor âconstexpr std::_Head_base<_Idx, _Head, false>::_Head_base(const _Head&) [with long unsigned int _Idx = 0ul, _Head = RSSFeed]â:
/usr/include/c++/4.6/tuple:162:44: instantiated from âconstexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(const _Head&, const _Tail& ...) [with long unsigned int _Idx = 0ul, _Head = RSSFeed, _Tail = {}]â
/usr/include/c++/4.6/tuple:423:24: instantiated from âconstexpr std::tuple<_T1>::tuple(const _T1&) [with _T1 = RSSFeed]â
/usr/include/c++/4.6/functional:1362:70: instantiated from âstd::_Bind_result<_Result, _Functor(_Bound_args ...)>::_Bind_result(_Functor&&, _Args&& ...) [with _Args = {RSSFeed&}, _Result = void, _Functor = void (*)(RSSFeed&), _Bound_args = {RSSFeed}]â
/usr/include/c++/4.6/functional:1477:41: instantiated from âtypename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type std::bind(_Functor&&, _ArgTypes&& ...) [with _Result = void, _Functor = void (&)(RSSFeed&), _ArgTypes = {RSSFeed&}, typename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â
/usr/include/c++/4.6/thread:135:9: instantiated from âstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(RSSFeed&), _Args = {RSSFeed&}]â
news-aggregator.cc:119:25: instantiated from here
rss-feed.h:54:3: error: âRSSFeed::RSSFeed(const RSSFeed&)â is private
/usr/include/c++/4.6/tuple:97:25: error: within this context
/usr/include/c++/4.6/tuple:97:25: error: use of deleted function âRSSFeed::RSSFeed(const RSSFeed&)â
rss-feed.h:54:3: error: declared here
rss-feed.h: In constructor âstd::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = RSSFeed, long unsigned int _Idx = 0ul, _Head = RSSFeed]â:
/usr/include/c++/4.6/tuple:174:43: instantiated from âstd::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(std::_Tuple_impl<_Idx, _Head, _Tail ...>&&) [with long unsigned int _Idx = 0ul, _Head = RSSFeed, _Tail = {}, std::_Tuple_impl<_Idx, _Head, _Tail ...> = std::_Tuple_impl<0ul, RSSFeed>]â
/usr/include/c++/4.6/tuple:434:51: instantiated from âstd::tuple<_T1>::tuple(std::tuple<_T1>&&) [with _T1 = RSSFeed, std::tuple<_T1> = std::tuple<RSSFeed>]â
/usr/include/c++/4.6/functional:1368:78: instantiated from âstd::_Bind_result<_Result, _Functor(_Bound_args ...)>::_Bind_result(std::_Bind_result<_Result, _Functor(_Bound_args ...)>&&) [with _Result = void, _Functor = void (*)(RSSFeed&), _Bound_args = {RSSFeed}, std::_Bind_result<_Result, _Functor(_Bound_args ...)> = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â
/usr/include/c++/4.6/functional:1477:41: instantiated from âtypename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type std::bind(_Functor&&, _ArgTypes&& ...) [with _Result = void, _Functor = void (&)(RSSFeed&), _ArgTypes = {RSSFeed&}, typename std::_Bindres_helper<_Result, _Functor, _ArgTypes>::type = std::_Bind_result<void, void (*(RSSFeed))(RSSFeed&)>]â
/usr/include/c++/4.6/thread:135:9: instantiated from âstd::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(RSSFeed&), _Args = {RSSFeed&}]â
news-aggregator.cc:119:25: instantiated from here
rss-feed.h:54:3: error: âRSSFeed::RSSFeed(const RSSFeed&)â is private
/usr/include/c++/4.6/tuple:101:42: error: within this context
/usr/include/c++/4.6/tuple:101:42: error: use of deleted function âRSSFeed::RSSFeed(const RSSFeed&)â
rss-feed.h:54:3: error: declared here
Resolved:
thread(parseFeed, ref( feed ) ); // line 119
This
RSSFeed feed();
is a function declaration that returns an object of type RSSFeed and has no parameters. It is not an object definition.
Write instead
RSSFeed feed;
Also it seems from the list of error messages that class RSSFeed has no the copy constructor that is it is defined as deleted.