can not compile my code with template and shared_ptr

237 views Asked by At

I have some code :

#include <list>
#include <iostream>
#include <string>


class A 
{
    private:
        std::string field1;
        std::string field2;
        
    public :
        A(std::string const& s1):field1(s1){};
        virtual ~A(){};
};


template<class CLIENT>
class B 
{
    private:
        A field1;
        A field2;
        CLIENT field3;
        
    public :
        B(A const&  a1):field1(a1){};
        virtual ~B(){};
        CLIENT muFunc(){return field3;};
};

int main(int argc, char** argv)
{
    std::string s("s");
    A a(s);
    std::make_shared<B<A>>(a);
    
    return 0;
}

when I tried to compile it, I get the following error :

main2.cc: Dans l'instanciation de « B::B(const A&) [with CLIENT = A] » : /opt/gcc_6_3/include/c++/6.3.0/ext/new_allocator.h:120:4: requis par « void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = B; _Args = {A&}; _Tp = B] » /opt/gcc_6_3/include/c++/6.3.0/bits/alloc_traits.h:455:4: requis par « static void std::allocator_traitsstd::allocator<_Tp1 >::construct(std::allocator_traitsstd::allocator<_Tp1 >::allocator_type&, _Up*, _Args&& ...) [with _Up = B; _Args = {A&}; _Tp = B; std::allocator_traitsstd::allocator<_Tp1 >::allocator_type = std::allocator<B >] » /opt/gcc_6_3/include/c++/6.3.0/bits/shared_ptr_base.h:520:39: requis par « std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {A&}; _Tp = B; _Alloc = std::allocator<B >; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u] » /opt/gcc_6_3/include/c++/6.3.0/bits/shared_ptr_base.h:615:4: requis par « std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = B; _Alloc = std::allocator<B >; _Args = {A&}; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u] » /opt/gcc_6_3/include/c++/6.3.0/bits/shared_ptr_base.h:1100:35: requis par « std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<B >; _Args = {A&}; _Tp = B; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u] » /opt/gcc_6_3/include/c++/6.3.0/bits/shared_ptr.h:319:64: requis par « std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<B >; _Args = {A&}; _Tp = B] » /opt/gcc_6_3/include/c++/6.3.0/bits/shared_ptr.h:619:14: requis par « std::shared_ptr<_Tp1> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = B; _Alloc = std::allocator<B >; _Args = {A&}] » /opt/gcc_6_3/include/c++/6.3.0/bits/shared_ptr.h:635:39: requis par « std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = B; _Args = {A&}] » main2.cc:37:26: requis depuis ici main2.cc:28:28: erreur : no matching function for call to « A::A() » B(A const& a1):field1(a1){}; ^ main2.cc:14:3: note : candidate: A::A(const string&) A(std::string const& s1):field1(s1){}; ^ main2.cc:14:3: note : candidate expects 1 argument, 0 provided main2.cc:7:7: note : candidate: A::A(const A&) class A ^ main2.cc:7:7: note : candidate expects 1 argument, 0 provided main2.cc:28:28: erreur : no matching function for call to « A::A() » B(A const& a1):field1(a1){}; ^ main2.cc:14:3: note : candidate: A::A(const string&) A(std::string const& s1):field1(s1){}; ^ main2.cc:14:3: note : candidate expects 1 argument, 0 provided main2.cc:7:7: note : candidate: A::A(const A&) class A ^ main2.cc:7:7: note : candidate expects 1 argument, 0 provided

I don't understand the error as the variable a is already created. Why does the compiler need a constructor without any paramter and how do I fix this issue.

thanks.

1

There are 1 answers

0
Ioan Le Gué On

sorry, I just put in the class A a default constructor without any parameter and it works....