-1

So far I have created a simple linked list project that I will build on in the future. All files compile correctly but when I try to build my project it comes up with a link error. I used an empty project to start. The main.cpp is used to demo that my linked list actually works. I'm really stuck and don't know how to solve this issue.

Main.cpp

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include "LinkedList.cpp"
#include "LinkedList.h"

using namespace SDI;

template <class T>
int LinkedList<T>::main()
{
    LinkedList<T> menu;             

    menu.insert(1);                     
    menu.insert(4);
    menu.insert(7);
    menu.insert(2);
    menu.insert(8);
    menu.display();                 
    Std::cout << "-----" << endl;
    menu.remove(2);                     
    menu.remove(1);
    menu.remove(10);
    menu.display();

    return 0;
};

header file LinkedList.h

#ifndef SDI_LL 
#define SDI_LL          

namespace SDI
{
    template < class T >                
    class LinkedList                
    {
        class Node                   
        {
            int number;             
            Node* next;            
        };

    private:

        T head;
        T current;        
        T temp;

    public:
        LinkedList();                                   
        ~LinkedList();                                  

        int main();
        void insert(int add);                           
        void remove(int remove);                       
        void display();                                 
    };

}
#endif

LinkedList.cpp

#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"

using namespace SDI;

template <class T>
LinkedList<T>::LinkedList()            
{
    head = NULL;
    current = NULL;                     
    temp = NULL;
};

template <class T>
void LinkedList<T>::insert(int add)         
{
    newnode = new Node;         
    newnode->next = NULL;                       
    newnode->number = add;                  

    if (head != NULL)                       
    {
        current = head;                     
        while (current->next != NULL)       
        {
            current = current->next;            
        }
        current->next = newnode;
    }
    else
    {
        head = newnode;                        
    }
};

template <class T>
void LinkedList<T>::remove(int remove)          
{
    remove1 = NULL;             
    temp = head;
    current = head;
    while (current != NULL && current->add != remove)           
    {
        temp = current;                                                     
        current = current->next;                                            
    }
    if (current == NULL)                                            
    {
        std::cout << "N/A\n";
        delete remove1;                                             
    }
    else
    {
        remove1 = current;                                          
        current = current->next;                                    
        temp->next = current;                                       
        if (remove1 == head)                                        
        {
            head = head->next;                                      
            temp = NULL;
        }

        delete remove1;
    }
};

template <class T>
void LinkedList<T>::display()
{
    current = head;                                                 

    while (current != NULL)                                         
    {
        std::cout << current->number;
        current = current->next;                                
    }
};
Timo Türschmann
  • 4,388
  • 1
  • 22
  • 30
Ryan
  • 3
  • 1
  • 2
  • 8
  • "All files compile correctly" - somehow I doubt that. In your `insert(int add)` definition, you use the variable `newnode`, but it has never been declared. That should be a compile error. Not to mention that your class variables `head`, `current` and `temp` will be of type `int` after instantiation, while at least some of them should probably be of type `Node`. There are plenty of bugs in your code. Edit: oh, of course, you never instantiated your class yet, so of course it "compiles" (by never creating any code) – Timo Türschmann Dec 11 '14 at 10:41
  • How would I go about fixing my bugs? I don't know how to instantiate my class? I have changed the node pointers to Node Head, Node current etc. How would I go about declaring the variable newnode in the best way? – Ryan Dec 11 '14 at 10:58
  • _@user3055196_ Also relevant: [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). @TimoTürschmann I'd guess the template code is simply not instantiated. – πάντα ῥεῖ Dec 11 '14 at 11:12
  • @user3055196 Template instantiation happens when you use the template. When you create a `main()` like the one in [Victor Sands answer](http://stackoverflow.com/a/27420614/1805439), your class will be instantiated with `T` as `int` in the statement `LinkedList menu;` – Timo Türschmann Dec 11 '14 at 11:30

1 Answers1

1

You need a global main() function in your program, not the static version from LinkedList. The main function is called the entry point, and a quick lookup of the error message tells you this.

http://en.cppreference.com/w/cpp/language/main_function

Something along the lines of this might work:

int main()
{
    LinkedList<int> menu;             

    menu.insert(1);                     
    menu.insert(4);
    menu.insert(7);
    menu.insert(2);
    menu.insert(8);
    menu.display();                 
    Std::cout << "-----" << endl;
    menu.remove(2);                     
    menu.remove(1);
    menu.remove(10);
    menu.display();

    return 0;
};

Currently, you define a completely unrelated "main" function inside your class. This is just a plain normal function of your class and in no way related to the previously mentioned int main() entry point function.

Victor Sand
  • 2,270
  • 1
  • 14
  • 32