compiling templates with g++

Amit Mehrotra amehrotr@uivlsi.csl.uiuc.edu
Tue Dec 21 12:12:00 GMT 1999


Please forgive me if this is obvious but I am new to C++.

I am having trouble compiling .cc files with templates. The thing
compiles but the .o file does not have the function code. When I link
it with a test program, it says that the function is not declared. The
funny thing is that when I merge .h and .cc into one .h, it compiles
and runs without any problem!  Pragmas did not help either. The files
and error messages are shown below:

I have two files quickSort.h and quickSort.cc:
----------------------------------quickSort.h------------------------------
#ifndef QUICKSORT_H
#define QUICKSORT_H
#include "vector.h" // decaration of Subscript type and Vector template class

template <class Type>
int quickSort(Vector<Type>&, Subscript, Subscript);
template <class Type>
int quickSort(Vector<Type>&, Vector<Type>&, Subscript, Subscript);

#endif // QUICKSORT_H

----------------------------------quickSort.cc-----------------------------
#include "quickSort.h"
template <class Type>
int quickSort(Vector<Type>& oof, Subscript poof, Subscript goof) {
// the implementation
}

template <class Type>
int quickSort(Vector<Type>& oof, Vector<Type>& poof, Subscript goof, 
        Subscript woof) {
// the implementation
}
---------------------------------error message----------------------------
/tmp/ccJjCAn0.o: In function `test(void)':
/tmp/ccJjCAn0.o(.text+0x416): undefined reference to `int quickSort<unsigned int>(Vector<unsigned int> &, unsigned int, unsigned int)'
collect2: ld returned 1 exit status



More information about the Gcc-help mailing list