-1

I am learning about creating a linked list in my class and I have run into a problem. My linked list is not a template and I can't figure out how to fix it. I looked at some of my notes for the class and the code looks to be the same as the slides but I was wondering if someone would be able to explain what I am doing wrong. Thank you so much.

LinkedList.h

#ifdef LINKED_LIST_
#define LINKED_LIST_

#include "ListInterface.h"
#include "Node.h"

template<class ItemType>
class LinkedList : public ListInterface<ItemType>
{
    private:
        Node<ItemType>* headPtr;
        int itemCount;

        Node<ItemType>* getNodeAt (int postion) const;
    
    public:
        LinkedList();
        LinkedList(const LinkedList<ItemType>& aList);
        virtual ~LinkedList();
        bool isEmpty() const;
        int getLength() const;
        bool remove(int postion);
        void clear();
        ItemType getEntry(int position) const;
        ItemType replace(int position, const ItemType& newEntry);

};

#include "LinkedList.cpp"
#endif

LinkedList.cpp

#include "LinkedList.h"
#include <iostream>

using namespace std;

template<class ItemType>
LinkedList<ItemType>::LinkedList() : headPtr(nullptr), itemCount(0){
    
}

template<class ItemType>
LinkedList<ItemType>::~LinkedList(){

}

The error only says: LinkedList is not a template.

Dylan
  • 1
  • 1
  • 1
    [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) You can do what you're attempting where you put the implementation in another file and include it, but it should not be named `.cpp` or compiled directly. – Retired Ninja Sep 28 '22 at 20:20
  • 1
    `#ifdef LINKED_LIST_` + `#define LINKED_LIST_` is not how you define a headerguard (and also doesn't make logical sense - why would you redefine the already defined preprocessor definition?) – UnholySheep Sep 28 '22 at 20:24
  • Delete LinkedList.cpp and place all the code in LinkedList.h – john Sep 28 '22 at 20:24
  • `#ifdef` does not do what you think it does. – Sam Varshavchik Sep 28 '22 at 20:25

1 Answers1

1
  1. Your header guards do no match. You want #ifndef or even #if !defined(LINKED_LIST_).
  2. It is inadvisable to #include the cpp file in the header file. If you must do so because your professor (unwisely) requires it, I suggest you rename it LinkedList.inl (short for "inline") or similar to distinguish it from a regular cpp file.
  3. The structure of your cpp file -- it has #include <iostream> and a using directive -- suggests you're trying to ALSO treat it as a compiled source file even though you directly included it above in the header. This will result in something like this after the preprocessor finishes copy-pasting the files around, which you can see live on Godbolt (modified since I don't have your other classes):
#include "ListInterface.h"
#include "Node.h"

template<class ItemType>
class LinkedList : public ListInterface<ItemType>
{
    private:
        Node<ItemType>* headPtr;
        int itemCount;

        Node<ItemType>* getNodeAt (int postion) const;
    
    public:
        LinkedList();
        LinkedList(const LinkedList<ItemType>& aList);
        virtual ~LinkedList();
        bool isEmpty() const;
        int getLength() const;
        bool remove(int postion);
        void clear();
        ItemType getEntry(int position) const;
        ItemType replace(int position, const ItemType& newEntry);

};

#include <iostream>

using namespace std;

template<class ItemType>
LinkedList<ItemType>::LinkedList() : headPtr(nullptr), itemCount(0){
    
}

template<class ItemType>
LinkedList<ItemType>::~LinkedList(){

}
//

#include <iostream>

using namespace std;

template<class ItemType>
LinkedList<ItemType>::LinkedList() : headPtr(nullptr), itemCount(0){
    
}

template<class ItemType>
LinkedList<ItemType>::~LinkedList(){

}

You can see I get the expected redefinition errors like:

<source>:41:23: error: redefinition of 'LinkedList<ItemType>'
  1. You might try adding a third source file that includes the header (and can see the definitions, preferably because they're defined inline in the header), and compile only that one source.
  2. Failing that, show us a minimal sample that reproduces the problem.
metal
  • 6,202
  • 1
  • 34
  • 49