Last semester I took CSII and we built an unsorted list (C++). This summer I went thru the textbook and built an sorted list, and now I'm trying to template the sorted list, but i keep getting compiler errors like this...

: undefined reference to `SortedType<float>::SortedType()'
/tmp/cc0C4kB6.o(.text+0x448): In function `main':
: undefined reference to `SortedType<float>::GetLength() const'
/tmp/cc0C4kB6.o(.text+0x497): In function `main':
: undefined reference to `SortedType<float>::InsertItem(float)'
/tmp/cc0C4kB6.o(.text+0x551): In function `main':
: undefined reference to `SortedType<float>::DeleteItem(float)'
/tmp/cc0C4kB6.o(.text+0x61c): In function `main':
: undefined reference to `SortedType<float>::FindItem(float)'
/tmp/cc0C4kB6.o(.text+0x6aa): In function `main':
: undefined reference to `SortedType<float>::MakeEmpty()'
/tmp/cc0C4kB6.o(.text+0x6e4): In function `main':
: undefined reference to `SortedType<float>::ResetList()'
/tmp/cc0C4kB6.o(.text+0x744): In function `main':
: undefined reference to `SortedType<float>::PrintList()'
/tmp/cc0C4kB6.o(.text+0x763): In function `main':
: undefined reference to `SortedType<float>::GetLength() const'
/tmp/cc0C4kB6.o(.text+0x79f): In function `main':
: undefined reference to `SortedType<float>::IsFull() const'
/tmp/cc0C4kB6.o(.text+0x80a): In function `main':
: undefined reference to `SortedType<float>::IsEmpty() const'
collect2: ld returned 1 exit status

I checked the web first and this definitely is a linker error. Recommendations were to check function spelling, file inclusion etc. Since I copied the program over from the sorted list that I built over the last few days and am just adding templates (that program works fine), I think I might be missing something...

Did you put the template code in a header file ?

Did you include that header file in the places where you use SortedType ?

Did you put the template code in a header file ?

Did you include that header file in the places where you use SortedType ?

Thanks a lot for the reply. First, sorry to everyone for not putting the code in. Nebie, and I just watched the video on etiquette.

Anyway, what I did was made it really 'basic' --copied the program to a new file and left in only the default constructor. Here's the code

for the .h file

#ifndef SORTEDTYPE3_H
#define SORTEDTYPE3_H

template<class Any>
class SortedType
{

public:

SortedType();
private:

  int length;
};
#endif

then Sorted3.cpp:

#include "SortedType3.h"
#include <iostream>
template<class Any>
SortedType<Any>::SortedType()
{
        length = 0;


}

then the driver testSorted.cpp

#include <iostream>
using namespace std;
#include "SortedType3.h"
int main()
{
        SortedType<int>list;



};

I took out the other functions, but I'm still getting the same error, but just forthe constructor:

g++ testSorted3.cpp Sorted3.cpp
/tmp/ccbFasTf.o(.text+0x120): In function `main':
: undefined reference to `SortedType<int>::SortedType()'
collect2: ld returned 1 exit status

The compiler needs the template code in scope (ie, the header file) if it is to instantiate the template for the required type, be it int or whatever.

The compiler needs the template code in scope (ie, the header file) if it is to instantiate the template for the required type, be it int or whatever.

Got it working. Thanks for the help! Total noob question, but thanks for taking the time...

Comments
Good job
This question has already been answered. Start a new discussion instead.