0

Ok so I am new to c++ and have a assignment to create a sorted linked list with provided headers sortedlist.h and the node.h of course. I dont need help with any code within methods only how to get it to where when I make my object in my main.cpp class I'm more used to java than c++. my issue is when I tried creating the sortedLinkedList object I get:

undefined reference to `linkedSortedList::linkedSortedList()'

I am not sure how to get the main file to recognize the object or maybe I set it up wrong this is my first c++ work so I am likely making some simple error. but the issue is I cant test my code within the object class until I get this working, anyways heres what I have.

my main.cpp is simple:

#include <cstdlib>
#include "LinkedSortedList.h"
#include <iostream>
using namespace std;


// ---------------------------------------------------------------------
// main() -- Load some values into the list and print it.  Then clear
//           the list and reload it.  Finally, print the values in the
//           list using getfirst, which should remove all the values
//           from the list.
// ---------------------------------------------------------------------
int main() {

  int value;           // Next list value

  // Create a list
  linkedSortedList<int> mylist;


  // Load some values
  mylist.insert(7);
  mylist.insert(3);
  mylist.insert(-2);
  mylist.insert(5);


  // Print the whole list
  cout << "Using print():" << endl;
  mylist.print();
  cout << endl;


  return 0;
}

and here is my linkedSortedList.cpp

    #include "linkedSortedList.h"
#include "LinkedNode.h"



template <class Elm> linkedSortedList<Elm>::linkedSortedList() {
    head = LinkedNode<Elm>();
    tail = LinkedNode<Elm>();
    tail = head.next;

}

/**linkedSortedList::linkedSortedList(const linkedSortedList& orig) {
}**/

template <class Elm> linkedSortedList<Elm>::~linkedSortedList() {
}



  // Clear the list.  Free any dynamic storage.
template <class Elm> void linkedSortedList<Elm>::clear(){

  }      

  // Insert a value into the list.  Return true if successful, false
  // if failure.
template <class Elm>  bool linkedSortedList<Elm>::insert(Elm newValue){
     LinkedNode<Elm> Temp = LinkedNode<Elm>(newValue,NULL);

    if(size = 0){
        head.value = newValue;
    }else{

        if(size = 1){
            head.next = &Temp;
        }else{

        tail.next = &Temp;


        }
        tail = Temp.next;
     }
    listLength++;
  }

  // Get AND DELETE the first element of the list, placing it into the
  // return variable "value".  If the list is empty, return false, otherwise
  // return true.
template <class Elm> bool linkedSortedList<Elm>::getfirst(Elm &returnvalue){
    *returnvalue = head.value;

  }

  // Print out the entire list to cout.  Print an appropriate message
  // if the list is empty.  Note:  the "const" keyword indicates that
  // this function cannot change the contents of the list.
 template <class Elm> void linkedSortedList<Elm>::print() const{
     LinkedNode<Elm> current = head;
     while(current.next->value != NULL){
         current.print();
         current = current.next;
     }

  }


  // Check to see if "value" is in the list.  If it is found in the list,
  // return true, otherwise return false.  Like print(), this function is
  // declared with the "const" keyword, and so cannot change the contents
  // of the list.
template <class Elm> bool linkedSortedList<Elm>::find(Elm searchvalue)const{

  }

  // Return the number of items in the list
template <class Elm>  int linkedSortedList<Elm>::size() const{
    return listLength;
  }

;

And my linkedSortedList.h

#ifndef LINKEDSORTEDLIST_H
#define LINKEDSORTEDLIST_H

#include "SortedList.h"
#include "LinkedNode.h"
#include <iostream>
using namespace std;

template <class Elm> class linkedSortedList: public SortedList <Elm>{
public:

    linkedSortedList();
    ~linkedSortedList();

  void clear();          

  // Insert a value into the list.  Return true if successful, false
  // if failure.
   bool insert(Elm newvalue);

  // Get AND DELETE the first element of the list, placing it into the
  // return variable "value".  If the list is empty, return false, otherwise
  // return true.
   bool getfirst(Elm &returnvalue);

  // Print out the entire list to cout.  Print an appropriate message
  // if the list is empty.  Note:  the "const" keyword indicates that
  // this function cannot change the contents of the list.
    void print() const;

  // Check to see if "value" is in the list.  If it is found in the list,
  // return true, otherwise return false.  Like print(), this function is
  // declared with the "const" keyword, and so cannot change the contents
  // of the list.
    bool find(Elm searchvalue) const;

  // Return the number of items in the list
    int size() const;

   private:
       LinkedNode<Elm> head;
       LinkedNode<Elm> tail;
       int listLength;

};

#endif  /* LINKEDSORTEDLIST_H */

Any help is appreciated like I said I am new to the language and am trying to learn so if you can help and wouldn't mind explaining a little that'd be great thanks in advance

  • 1
    Why post all the code when the problem is with the compiler? So is it visual studio, g++, .... Using makefile, scons, gradle, ... – Ed Heal Feb 26 '14 at 23:16
  • You are including `linkedSortedList.h` in `linkedSortedList.cpp` but `LinkedSortedList.h` in `main.cpp`. Is this a question typo or do you have both files? –  Feb 26 '14 at 23:18

1 Answers1

1

The problem is, that you are defining a template in the cpp file. You need to either move them in the header file, or include the cpp file in your main file.

Due to SFINAE, a template resolution cannot be performed by the compiler without having access to the definition of each function, while compiling your main file.

utnapistim
  • 26,809
  • 3
  • 46
  • 82
  • so what needs to be added to the header and where do I need to add it are you saying move the `template ` to the methods in the header instead of the .cpp file – user3290875 Feb 26 '14 at 23:38
  • 1
    Take all the method definitions from the linkedsortedlist.cpp and move it to the header file, under the class definition. Then, remove linkedsortedlist.cpp from your project. – utnapistim Feb 27 '14 at 00:09