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...
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...
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
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...
Hey everyone.
Last spring I completed my CSII course and will be taking data structures and algorithms in the fall. I came into my CS program with no prior programming experience but have managed A's in the first year, but from what I've been told Data Structures is a pretty tough course so I'm preparing this summer for the fall.
What I'm doing is working my way through both my last semester's textbook, "C++ and Data Structures " by Nell Dale --the parts we didn't cover--and going through all of the "C++ Primer Plus" by Stephen Prata.
I'm half way through the Primer Plus and have started looking at some of the 'case studies' in my textbook (for those familiar with it).
Just wanted to introduce myself because I'll probably have some questions in the coming week and thereafter. Just to let anyone know, I'm not in class right now and don't need help with 'homework', and won't be asking people to code my stuff. Just looking to build knowledge over the summer.
Thanks,
Matt