vendredi 15 janvier 2021

How to fix linking error (undefined reference) with importing header file into driver program? [duplicate]

I'm visiting an old program I wrote awhile back, I noticed my class implementation was written in my header file so I am refactoring into seperate .h and .cpp files. The code worked before when it was simply the driver program and my .h, but now it seems something is broken when I use g++.

Here are the files in my directory:

CircularDoublyLinkList.cpp CircularDoublyLinkList.h DoubleNode.h driver.cpp

My driver.cpp:

#include "CircularDoublyLinkList.h"

int main() {
// instantiating Circular_double_list<string>
// objects and using their member functions to do stuff...
}

First header file DoubleNode.h:

#ifndef DOUBLE_NODE
#define DOUBLE_NODE

template <class T>
class Double_node {

    public:

        T node_value;

        Double_node<T>* previous_node;

        Double_node<T>* next_node;

        Double_node() {}; //default contructor

        Double_node(T const & object, Double_node * prev = nullptr, Double_node * next = nullptr):
            node_value(object),
            previous_node(prev),
            next_node(next) 
            {};

        ~Double_node() {}; // default destructor

        T value() const { return node_value; }

        Double_node* next() const { return next_node; }

        Double_node* previous() const { return previous_node; } 

};
#endif

Second header file CircularDoublyLinkList.h:

#ifndef CIRCULAR_DOUBLE_LIST_H
#define CIRCULAR_DOUBLE_LIST_H

#include "DoubleNode.h"
#include <stdexcept>
#include <iostream>

template <class Type>
class Circular_double_list {

private:
    Double_node<Type>* list_head;
    int list_size;

public:

    Circular_double_list() : list_head(nullptr), list_size(0) {}

    Circular_double_list(const Circular_double_list &);

    ~Circular_double_list() {
        clear();
    }

    //accessors prototypes
    int size() const { return list_size; }
    bool empty() const { return !list_size; }
    Type front() const;
    Type back() const;
    Type at(int) const;
    Double_node<Type> *end() const { return list_head->previous_node; }
    Double_node<Type> *head() const { return list_head; }
    int count(Type const &) const;

    // operators
    void operator = (const Circular_double_list &);
    bool operator == (const Circular_double_list &);
    bool operator != (const Circular_double_list &);

    //mutators prototypes   
    void swap(const Circular_double_list &);
    void push_front(Type const &);
    void push_back(Type const &);
    Type pop_front();
    Type pop_back();
    void insertAt(int, Type const &);
    void eraseAt(int);
    int find(Type const &);
    void clear();
};

#endif

My class implementation for Link List CircularDoublyLinkList.cpp:


#include "CircularDoublyLinkList.h"
#include <stdexcept>
#include <iostream>

...
// Defining my member functions.. 

Here's my error message when I compile my code:

 g++ driver.cpp CircularDoublyLinkList.cpp
/usr/bin/ld: /tmp/ccusMJPb.o: in function `main':
driver.cpp:(.text+0x4da): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::front() const'
/usr/bin/ld: driver.cpp:(.text+0x55b): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::back() const'
/usr/bin/ld: driver.cpp:(.text+0x6b3): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::at(int) const'
/usr/bin/ld: driver.cpp:(.text+0x7e3): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::count(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/usr/bin/ld: driver.cpp:(.text+0x881): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::swap(Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)'
/usr/bin/ld: driver.cpp:(.text+0x994): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push_front(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: driver.cpp:(.text+0xa89): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push_back(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: driver.cpp:(.text+0xaf0): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::pop_front()'
/usr/bin/ld: driver.cpp:(.text+0xb9b): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::pop_back()'
/usr/bin/ld: driver.cpp:(.text+0xce8): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::insertAt(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: driver.cpp:(.text+0xd94): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::eraseAt(int)'
/usr/bin/ld: driver.cpp:(.text+0xe18): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::find(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: driver.cpp:(.text+0xe85): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::clear()'
/usr/bin/ld: /tmp/ccusMJPb.o: in function `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~Circular_double_list()':
driver.cpp:(.text._ZN20Circular_double_listINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEED2Ev[_ZN20Circular_double_listINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEED5Ev]+0x18): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::clear()'
/usr/bin/ld: /tmp/ccusMJPb.o: in function `void print<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)':
driver.cpp:(.text._Z5printINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvRK20Circular_double_listIT_E[_Z5printINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEvRK20Circular_double_listIT_E]+0x4f): undefined reference to `Circular_double_list<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::at(int) const'
collect2: error: ld returned 1 exit status

There must be something I'm just not seeing...

Aucun commentaire:

Enregistrer un commentaire