0
Though I am compiling all the cpp files and linking the object files I am getting the following error
g++ -c simple_list.cpp 
g++ -c test_simple_list.cpp
g++ -o test_simple_list simple_list.o test_simple_list.o 
/usr/bin/ld: test_simple_list.o: in function 'main':
test_simple_list.cpp:(.text+0x24): undefined reference to 'list<int>::list()'
/usr/bin/ld: test_simple_list.cpp:(.text+0x46): undefined reference to 'list<int>::push_front(int const&)'

/usr/bin/ld: test_simple_list.cpp:(.text+0x64): undefined reference to `std::ostream& operator<< (std::ostream&, list&)' /usr/bin/ld: test_simple_list.cpp:(.text+0x70): undefined reference to 'list::~list()' /usr/bin/ld: test_simple_list.cpp:(.text+0x99): undefined reference to 'list::~list()' collect2: error: ld returned 1 exit status

simple_list.h file
    #ifndef __LIST__H__
    #define __LIST__H__

    #include <iostream>

    #include <limits>

    template < typename Elem >
        struct Link {
            Link * succ; // successor (next) link
            Elem val; // the value
        };

    template < typename Elem >
        class list {
            private:
                unsigned int lsize;
            Link < Elem > * first;
            Link < Elem > * last; // one beyond the last link
            public:
                class iterator;
            list();
            list & operator = (const list & obj);
            bool empty();
            unsigned int size();
            iterator begin();
            iterator end();
            iterator insert_after(iterator p,
                const Elem & v);
            iterator erase_after(iterator p);
            void push_front(const Elem & v);
            Elem & front();
            unsigned int max_size();
            ~list();
        };
    
    template < typename Elem >
        std::ostream & operator << (std::ostream & os, list < Elem > & lt);
    
    #endif

    ```
    
    simple_list.cpp
    
    ```
    #include "simple_list.h"
    
    template < typename Elem >
        class iterator {
            private:
                Link < Elem > * curr;
            public:
                iterator(Link < Elem > * curr) {
                    this -> curr = curr;
                }
            iterator & operator++() {
                curr = curr -> succ;
                return *this;
            }
            Elem & operator * () {
                return curr -> value;
            }
    
            friend class list < Elem > ;
        };
    
    template < typename Elem >
        list < Elem > ::list() {
            first = last = new Link < Elem > ;
        }
    
    template < typename Elem >
        list < Elem > & list < Elem > ::operator = (const list < Elem > & obj) {
            typename list < Elem > ::iterator i = obj.begin();
            typename list < Elem > ::iterator j = begin();
            if (i == obj.begin()) {
                push_front( * i);
            }
            while (i != obj.end()) {
                i++;
                insert_after(j, * i);
                j++;
            }
            return *this;
        }
    
    template < typename Elem >
        bool list < Elem > ::empty() {
            if (size() == 0)
                return false;
            return true;
        }
    
    template < typename Elem >
        unsigned int list < Elem > ::size() {
            return size;
        }
    
    template < typename Elem >
        typename list < Elem > ::iterator list < Elem > ::begin() {
            return typename list < Elem > ::iterator(first);
        }
    
    template < typename Elem >
        typename list < Elem > ::iterator list < Elem > ::end() {
            return iterator(last);
        }
    
    template < typename Elem >
        typename list < Elem > ::iterator list < Elem > ::erase_after(typename list < Elem > ::iterator p) {
            Link < Elem > * curr = p.curr;
            Link < Elem > * deleteLink = p.curr -> succ;
            //Link<Elem> *deleteLinkSucc = deleteLink->succ;
            p++;
            if (p == end())
                return end();
            lsize--;
            curr -> succ = p.curr;
            return p;
        }
    
    template < typename Elem >
        typename list < Elem > ::iterator list < Elem > ::insert_after(typename list < Elem > ::iterator p,
            const Elem & v) {
            Link < Elem > * newLink = new Link < Elem > ;
            newLink -> val = v;
            newLink -> succ = p.curr -> succ;
            p.curr -> succ = newLink;
            p++;
            lsize++;
            return p;
        }
    
    template < typename Elem >
        void list < Elem > ::push_front(const Elem & v) {
            Link < Elem > * newLink = new Link < Elem > ;
            newLink -> val = v;
            newLink -> succ = first;
            first = newLink;
            lsize++;
        }
    
    template < typename Elem >
        Elem & list < Elem > ::front() {
            return first -> val;
        }
    
    template < typename Elem >
        unsigned int list < Elem > ::max_size() {
            return std::numeric_limits < unsigned int > ::max();
        }
    
    template < typename Elem >
        list < Elem > ::~list() {
            while (first != last) {
                Link < Elem > * temp = first -> succ;
                delete first;
                first = temp;
            }
            delete last;
        }
    
    template < typename Elem >
        std::ostream & operator << (std::ostream & os, list < Elem > & lt) {
            for (typename list < Elem > ::iterator i = lt.begin(); i != lt.end(); i++)
                os << * i << " ";
            return os;
        }
    ```
    test_simple_list.cpp file
    
    ```
    #include "simple_list.h"
    
    int main() {
        list < int > a;
        for (int i = 1; i <= 5; i++)
            a.push_front(i);
    
        std::cout << a;
    
    }
    ```
praful h y
  • 45
  • 4

0 Answers0