C ++ variation patterns unresolved external virtual abstract

Today I wrote the code for my project and received an unresolved external from the linker, the code should generate a class with several virtual abstract methods - as a base for class collections. Therefore, I decided to use variation templates for this task, but I got an error.

template<int I>
struct pin_tag {};

//inputs
template<int N, class T0, class... VAR>
class inputs_base : public inputs_base<N + 1, VAR...>
{
protected:
   typedef inputs_base<N + 1, VAR...> base_type;
   using arg_type = T0;
   //using base_type::_in;
   virtual void _in(T0 const& t, pin_tag<N>) = 0;
};

template<int N, class T0>
class inputs_base<N, T0>
{
protected:
   using arg_type = T0;
   virtual void _in(T0 const& t, pin_tag<N>) = 0;
};

template<class... VAR>
class inputs : public inputs_base<0, VAR...>
{
private:
   using inputs_type = inputs<VAR...>;
   using inputs_base_type = inputs_base<0, VAR...>;

   template <int N, class T = inputs_type>
   struct getter
   {
     using next_type = typename getter<N - 1, typename T::base_type>;
     using arg_type = typename next_type::arg_type;
     using current_type = typename next_type::current_type;
   };

   template <class T>
   struct getter<0, T>
   {
     using arg_type = typename T::arg_type;
     using current_type = typename T;
   };

   public:
   template<int N>
   using in_arg_type = typename getter<N>::arg_type;

   template<int N>
   void in(in_arg_type<N> const& t)
   {
     getter<N>::current_type::_in(t, pin_tag<N>());
   }
};


class test : public inputs< int, bool >
{
protected:
   virtual void _in(int const& val, pin_tag<0>)
   {}
   virtual void _in(bool const& val, pin_tag<1>)
   {}
 };

int _tmain(int argc, _TCHAR* argv[])
{

   test t;
   t.in<0>(100500);
}

I have a linker error

error LNK2019: unresolved external symbol "protected: virtual void __thiscall inputs_base<0,int,bool>::_in(int const &,struct pin_tag<0>)" (?_in@?$inputs_base@$0A@H_N@@MAEXABHU?$pin_tag@$0A@@@@Z) referenced in function "public: void __thiscall inputs<int,bool>::in<0>(int const &)" (??$in@$0A@@?$inputs@H_N@@QAEXABH@Z)

It seems to have _inlost virtuality. Can anyone check my code please.

+4
source share
1 answer

Solution to the problem

In inputs<...>::in()replace

getter<N>::current_type::_in(t, pin_tag<N>());

by

this->_in(t, pin_tag<N>());

, inputs_base,

using base_type::_in;

.

.

_in " ", getter<N>::current_type. this->_in(...) .

_in ; using . , _in . .


, std:: tuple_element (, ) getter. , , getter inputs (, ) ""; .

,

using inputs_type = inputs<VAR...>;

. inputs , <VAR...>. inputs.

- . inputs_base inputs 51 21 .

inputs_base : _in(). , in() , getter, std::tuple_element. , inputs

template<class... U>
struct inputs : inputs_base<0, U...>
{
    template<int N, typename T>
    void in(T const& t) { this->_in(t, pin_tag<N>()); }
};


, _in void , const. . .

- . . test :

struct test : overload<void(int), void(bool)>
// ...

, . int bool; const&.

+3

Source: https://habr.com/ru/post/1534109/


All Articles