-4

I'm building a C++ list program. There is an ADT List which is a purely virtual template class, which SLL (singly linked list) inherits from. I've written the class definition in sll.h and attempted to implement the list in sll.cpp. However I keep getting the following two errors,

1)

In file included from cpp_files/sll.cpp:1:0,
                 from main.cpp:3:
cpp_files/../includes/sll.h:3:25: error: expected class-name before ‘{’ token
 class SLL : public List {

2)

cpp_files/../includes/sll.h:12:54: error: cannot declare member function ‘List<L>::insert’ within ‘SLL’
        void List<L>::insert( L element, int position );

My question, what is going on? Why does this not work?

SLL.cpp
#include "../includes/sll.h"
/*
 Singly Linked List Implementation
*/
SLL::SLL() {}

SLL::~SLL() {}

template <class L>
void List<L>::insert( L element, int position ) {

}
SLL.H
#include "../includes/list.h"

class SLL : public List {
    private:
    public:

       SLL();

       ~SLL();

       template <class L>
       void List<L>::insert( L element, int position );

};

List.h

#ifndef LIST_H
#define LIST_H

/*
In this code we define the headers for our ADT List.
*/
template<class L>
class List {

private:

public: // This is where functions go

  typedef struct node {
      int data;
      node* next;
  } * node_ptr;

  virtual void insert( L element, int position ) = 0;

};


#endif // LIST_H
Giga Chad Coding
  • 178
  • 3
  • 12
  • 1
    Not your actual problem, but you urgently need to read [this](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – user0042 Sep 10 '17 at 14:52
  • 2
    Show us your code (the smallest self-contained amount that demonstrates the problem), and not just the error message. – NPE Sep 10 '17 at 14:52
  • check my edit, my bad on that – Giga Chad Coding Sep 10 '17 at 14:56

1 Answers1

2

List is a template so you need to specify a template parameter

class SLL : public List // no template parameter specified!

Needs to be something like

class SLL : public List<int> // now we have a complete type

Or you need to add a template parameter to SLL

template<class L>
class SLL : public List<L> // now the user of SLL tells us the complete type

Also you can not put part of the template definition in a separate cpp file so if you make SLL a template class you need to put its whole definition in the header. The same with all your templates.

Galik
  • 47,303
  • 4
  • 80
  • 117