Calling function that was declared virtual in interface (and implemented in derived class) inside base abstract class

  abstract-class, c++, inheritance, interface

I have the following inheritance model:

   interface      abstract class   concrete derived class
_________________________________________________________
IPriorityQueue -> APriorityQueue -> UnsortedPriorityQueue

My member function was declared purely virtual in the interface:

template <typename K, typename V>
class   IPriorityQueue
{
        ...
        virtual int     size(void)  const = 0;
        virtual bool    empty(void) const = 0;
};

In the abstract class, I want to use size() to already implement empty(), since if size = 0, then the priority queue is empty:

template <typename K, typename V>
class   APriorityQueue : virtual public IPriorityQueue<K, V>
{
        ...
        bool    empty(void) const { return (!size()); }
};

size() is properly implemented in the derived class:

template <typename K, typename V>
int UnsortedPriorityQueue<K, V>::size(void) const
{
    return (this->_list.size());  // _list is a private member which is an STL list implementation
}

However, I get the following error:

../../libft/APriorityQueue.hpp:49:37: error: there are no arguments to 'size' that depend on a template parameter, so a declaration of 'size' must be available [-fpermissive]
   bool empty(void) const { return (!size()); }
                                     ^~~~
../../libft/APriorityQueue.hpp:49:37: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)

I read in some other answers on StackOverflow that one has to specify the namespace, so I modified it the following way:

        bool    empty(void) const { return (!IPriorityQueue<K, V>::size()); }

But now I get a linker error complaining that IPriorityQueue<K, V>::size() is not implemented:

main.o:main.cpp:(.text$_ZNK14APriorityQueueIiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE5emptyEv[_ZNK14APriorityQueueIiNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE5emptyEv]+0x28): undefined reference to `IPriorityQueue<int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::size() const'
collect2.exe: error: ld returned 1 exit status

Is there any way I can figure this out? Is such a design even possible? Thank you in advance

Source: Windows Questions C++

LEAVE A COMMENT