Linker probs with template classes
Thomas Matelich
tmatelich at zetec.com
Fri Jan 7 19:51:16 GMT 2000
I must make an obligatory note about this being offtopic, a better place would be
the g++ mailing list.
Anyway, so that the compiler can instantiate the class (i.e. create the machine
code) for your int array, it must be able to find the source code for the template
function. That means one of two options. 1) put the code in the header. 2)
#include your source file from your header.
Tom
Werner Modenbach wrote:
> Hi there!
>
> Maybe there is someone who can give me a hint on my problem (I spent allready
> days on it :-(( )
>
> I have a sinple template class for a circular array and I reduced it to a
> minimum:
> -----------------------------------------------
> circularArray.h:
>
> template <class T>
> class circularArray {
> public:
> circularArray(int size) : arraySize(size){};
> ~circularArray(){};
> protected:
> T* Elements;
> int arraySize;
> };
>
> -----------------------------------------------
> main.cpp:
>
> #ifdef HAVE_CONFIG_H
> #include <config.h>
> #endif
>
> #include <iostream.h>
> #include <stdlib.h>
> #include <circularArray.h>
>
> int main(int argc, char** argv){
>
> circularArray<int> a(10);
>
> cout << "Hello, World!" << endl;
> return EXIT_SUCCESS;
> }
> -----------------------------------------------
>
> The program works fine so far.
> If i change the definition of the constructor to:
>
> -----------------------------------------------
> in circularArray.h:
> circularArray(int size);
> -----------------------------------------------
>
> Create circularArray.cpp:
>
> #include <circularArray.h>
>
> template <class T>
> circularArray<T>::circularArray(int size){
> arraySize = size;
> }
>
> The following messages apear in the log window:
>
> c++ -DHAVE_CONFIG_H -I. -I. -I.. -O0 -g3 -Wall -c circularArray.cpp
> c++ -DHAVE_CONFIG_H -I. -I. -I.. -O0 -g3 -Wall -c main.cpp
> c++ -O0 -g3 -Wall -o circular_test circularArray.o main.o
> main.o: In function `circularArray<int>::~circularArray(void)':
> /home/werner/circular_test/circular_test/circularArray.h:17: undefined reference
> to `circularArray<int>::circularArray(int)' gmake: *** [circular_test] Error 1
> *** Fehler ***
>
> circularArray.h:17 is the line where the destructor is defined;
> ~circularArray(){};
>
> If I define more methods of the class everything is fine as far as I define
> everything in the headerfile.
>
> Sorry for the OT question! I really have no idea.
>
> - Werner -
>
> --
> ---------------------------------------
> Werner Modenbach modenbach at alc.de
> ALC Computertechnik GmbH http://www.alc.de
--
Thomas O Matelich
Senior Software Designer
Zetec, Inc.
sosedada at usa.net
tmatelich at zetec.com
More information about the KDevelop
mailing list