1

SortedList.h is an abstract template (pure virtual functions), LinkedSortedList.h derives SortedList.h and is a template, and LinkedSortedList.cpp is a template implementing the functions in LinkedSortedList.h.

My Error is when I'm trying to override the functions from SortedList.h in LinkedSortedList.h.

Method in SortedList.h:

virtual void clear() = 0;   

Method in LinkedSortedList.h:

template <typename Elm> void SortedList<Elm>::clear() override; 

error:

error C3240: 'clear' : must be a non-overloaded abstract member function of 'SortedList'

error C2838: 'clear' : illegal qualified name in member declaration

I've tried:

void SortedList<Elm>::clear(){}

But I still get the same error. I've tried to find the solution on the web, but have failed.

Here is SortedList.h It must not be changed:

#ifndef _SortedListClass_
#define _SortedListClass_

template <class Elm> class SortedList {
public:

  // -------------------------------------------------------------------
  // Pure virtual functions -- you must implement each of the following
  // functions in your implementation:
  // -------------------------------------------------------------------

  // Clear the list.  Free any dynamic storage.
  virtual void clear() = 0;          
                                 
  // Insert a value into the list.  Return true if successful, false
  // if failure.
  virtual bool insert(Elm newvalue) = 0;
            
  // Get AND DELETE the first element of the list, placing it into the
  // return variable "value".  If the list is empty, return false, otherwise
  // return true.
  virtual bool getfirst(Elm &returnvalue) = 0;

  // Print out the entire list to cout.  Print an appropriate message
  // if the list is empty.  Note:  the "const" keyword indicates that
  // this function cannot change the contents of the list.
  virtual void print() const = 0;

  // Check to see if "value" is in the list.  If it is found in the list,
  // return true, otherwise return false.  Like print(), this function is
  // declared with the "const" keyword, and so cannot change the contents
  // of the list.
  virtual bool find(Elm searchvalue) const = 0;

  // Return the number of items in the list
  virtual int size() const = 0;
};

#endif

Here is LinkedSortedList.h This can be rewritten if needed:

#ifndef _LinkedSortedList_
#define _LinkedSortedList_

#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>

using namespace std;

template <class Elm> class LinkedSortedList:public SortedList<Elm>
{
public:
    template <typename Elm> LinkedSortedList();
    virtual void SortedList<Elm>::clear();          
    virtual bool SortedList<Elm>::insert(Elm newvalue);
    virtual bool SortedList<Elm>::getfirst(Elm &returnvalue);
    virtual void SortedList<Elm>::print() const;
    virtual bool SortedList<Elm>::find(Elm searchvalue) const;
    virtual int SortedList<Elm>::size() const;

private:
    LinkedNode<Elm> *head;
    int num;
};

#endif

LinkedSortedList.cpp is the implementation for LinkedSortedList, and has these two lines at the end:

template class LinkedSortedList<int>;
template class LinkedSortedList<double>;
Community
  • 1
  • 1
Delliardo
  • 175
  • 2
  • 3
  • 15
  • I'm trying to decide if this applies: http://stackoverflow.com/q/2354210/10077 – Fred Larson Feb 25 '14 at 19:21
  • My lack of experience with C++ means I'm blind to what I'm seeing on that page. Could you condense it down for me? – Delliardo Feb 25 '14 at 19:26
  • 1
    Related: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Zac Howland Feb 25 '14 at 19:26
  • Are you trying to override `clear()` as a member of a `LinkedSortedList` class? Because you're declaring it as a member of `SortedList`, which seems weird. I think you just want `void clear() override {}`. It would help to see more of each class declaration. – Fred Larson Feb 25 '14 at 19:30
  • @zac: already did that in LinkedSortedList.cpp. – Delliardo Feb 25 '14 at 19:33
  • @FredLarson, Posted the files. I was told in another question not to do this, so sorry for the lack of info. :P – Delliardo Feb 25 '14 at 19:40
  • @Delliardo I think you misunderstand what is being said in those responses. Additionally, there is no need for your abstract base class to be a template. It is defining an interface that has nothing to do with a type. – Zac Howland Feb 25 '14 at 19:54
  • @ZacHowland: What about `Elm`? It's an argument to several of the virtual functions. – Fred Larson Feb 25 '14 at 20:00
  • @FredLarson Good point ... I was skimming too fast and missed those :( – Zac Howland Feb 25 '14 at 20:02
  • @πάνταῥεῖ: I think the edit makes it clear it is NOT a duplicate of that question, as this question now shows the explicit instantiation recommended in answers to that question. – Fred Larson Feb 25 '14 at 20:07

1 Answers1

1

You're overriding incorrectly. The overrides are members of your derived class, not the base class.

Also, there's no reason for your constructor to be a function template.

I think your class should look more like this:

template <class Elm> class LinkedSortedList : public SortedList<Elm>
{
public:
    LinkedSortedList();
    void clear() override;          
    bool insert(Elm newvalue) override;
    bool getfirst(Elm &returnvalue) override;
    void print() const override;
    bool find(Elm searchvalue) const override;
    int size() const override;

private:
    LinkedNode<Elm> *head;
    int num;
};
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • And that fixed it. I swear I did that, or at least I think I tried that. Thank you for your help, this has been quite the learning experience for me. Now to implement the functions! – Delliardo Feb 25 '14 at 20:03