0

I am working on coding all of the basic data structures in c++, and I have started with a LinkedList.

I have LinkedList.h which is the class definition for a LinkedList, with a nested LinkedNode class defintion. LinkedList.cpp and LinkedNode.cpp hold the actual function implementations for all of the functions in the classes as dictated by LinkedList.h. I also have LinkedListTests.h and LinkedListTests.cpp, where LinkedListTests.h has a set of functions which test my LinkedList implementation, and LinkedListTests.cpp has a main function which invokes the functions from LinkedListTests.h.

All of the .cpp files have:

#include "LinkedList.h"

in their headers, and LinkedListTests.cpp also has:

#include "LinkedListTests.h"

in it.

This is the makefile I created to compile and link the entire project together:

LinkedList: LinkedListTests.o LinkedList.o LinkedNode.o
    g++ LinkedListTests.o LinkedList.o LinkedNode.o -o LinkedList

LinkedListTests.o: LinkedListTests.cpp LinkedListTests.h LinkedList.h
    g++ -c LinkedListTests.cpp

LinkedList.o: LinkedList.cpp LinkedList.h
    g++ -c LinkedList.cpp

LinkedNode.o: LinkedNode.cpp LinkedList.h
    g++ -c LinkedNode.cpp

clean:
    rm *.o LinkedList

Any help as to why I get undefined reference errors? I believe all of the .cpp files should be compiled and linked together, but that doesn't appear to be the case.

For reference, I am compiling and running with g++ on a Kali Linux virtual machine.

  • 1
    Only header files (`.h`) should have `#pragma once`, not implementation (`.cpp`) files. – cigien May 31 '20 at 20:12
  • 1
    What happens if you do `g++ LinkedListTests.cpp LinkedList.cpp LinkedNode.cpp -o LinkedList` ? You have neither included the error message nor the source code in your post. With that information it would be easy to help you. – user2672165 May 31 '20 at 20:35
  • If your linked list is a template make sure you implemented it in the header. – drescherjm May 31 '20 at 21:11

0 Answers0