J
Jacob
A problem I've been rather stuck on and, try as I might, have not been
able to find the answer to.
I've written a nice little class that I want to include in another
project. The class works fine if I have it all in one big file, but I run
into problems when I try to break it down into source and header files. I
get a linker error when I try to use a member function of a class that
uses a template. I'm sure it just has to do with the way I'm breaking it
down into .cpp and .h files, but I don't know where I'm going wrong.
Actually, I'm pretty sure I do know where I'm going wrong, but I don't
know how to go "right".
In trying to solve the problem myself, I threw together a quick test
program. I'm using one cpp file and a Makefile. (I'm using GCC 3.2.2 by
the way)
Here's the entire contents of the Makefile (in the one that builds and
runs properly):
<SNIP>
PROGNAME = test
test: someclass.cpp
g++ -Wall someclass.cpp -o $(PROGNAME)
</SNIP>
and here's "someclass.cpp" in it's entirety:
<SNIP>
#include <stdio.h>
template <class sometype>
class testclass{
public:
sometype value;
sometype blah(sometype newvalue);
};
template <class sometype>
sometype testclass<sometype>::blah(sometype newvalue){
value = newvalue;
printf("%i\n", (int)value);
return value;
}
int main(void){
testclass<int> bob;
bob.blah(7);
bob.blah(8);
return 0;
}
</SNIP>
If I make that it works fine. I run it and get the obvious results of the
7 and 8 being printed out on the screen.
When I break it down into headers and such I run into problems. Here's
the new Makefile:
<SNIP>
PROGNAME = test
test: main.cpp someclass.cpp
g++ -Wall main.cpp someclass.cpp -o $(PROGNAME)
</SNIP>
Here's someclass.cpp:
<SNIP>
#include <stdio.h>
#include "someclass.h"
template <class sometype>
sometype testclass<sometype>::blah(sometype newvalue){
value = newvalue;
printf("%i\n", (int)value);
return value;
}
</SNIP>
Here's someclass.h:
<SNIP>
#ifndef SOMECLASS_H
#define SOMECLASS_H
template <class sometype>
class testclass{
public:
sometype value;
sometype blah(sometype newvalue);
};
#endif //ifndef SOMECLASS_H
</SNIP>
and then here's main.cpp:
<SNIP>
#include "someclass.h"
int main(void){
testclass<int> bob;
bob.blah(7);
bob.blah(8);
return 0;
}
</SNIP>
When I try to "make" that one, I get the following linker errors:
<SNIP>
$ make
g++ -Wall main.cpp someclass.cpp -o test
/tmp/ccEyCxBU.o(.text+0x1a): In function `main':
: undefined reference to `testclass<int>::blah(int)'
/tmp/ccEyCxBU.o(.text+0x2b): In function `main':
: undefined reference to `testclass<int>::blah(int)'
collect2: ld returned 1 exit status
make: *** [test] Error 1
</SNIP>
Can anyone tell me what I'm doing wrong? I've tried quite a few different
ways of declaring the "blah" function in that header, but none of them
seem to work (I assume that's where the problem lies; it certainly seems
to be).
Thanks in advance!
able to find the answer to.
I've written a nice little class that I want to include in another
project. The class works fine if I have it all in one big file, but I run
into problems when I try to break it down into source and header files. I
get a linker error when I try to use a member function of a class that
uses a template. I'm sure it just has to do with the way I'm breaking it
down into .cpp and .h files, but I don't know where I'm going wrong.
Actually, I'm pretty sure I do know where I'm going wrong, but I don't
know how to go "right".
In trying to solve the problem myself, I threw together a quick test
program. I'm using one cpp file and a Makefile. (I'm using GCC 3.2.2 by
the way)
Here's the entire contents of the Makefile (in the one that builds and
runs properly):
<SNIP>
PROGNAME = test
test: someclass.cpp
g++ -Wall someclass.cpp -o $(PROGNAME)
</SNIP>
and here's "someclass.cpp" in it's entirety:
<SNIP>
#include <stdio.h>
template <class sometype>
class testclass{
public:
sometype value;
sometype blah(sometype newvalue);
};
template <class sometype>
sometype testclass<sometype>::blah(sometype newvalue){
value = newvalue;
printf("%i\n", (int)value);
return value;
}
int main(void){
testclass<int> bob;
bob.blah(7);
bob.blah(8);
return 0;
}
</SNIP>
If I make that it works fine. I run it and get the obvious results of the
7 and 8 being printed out on the screen.
When I break it down into headers and such I run into problems. Here's
the new Makefile:
<SNIP>
PROGNAME = test
test: main.cpp someclass.cpp
g++ -Wall main.cpp someclass.cpp -o $(PROGNAME)
</SNIP>
Here's someclass.cpp:
<SNIP>
#include <stdio.h>
#include "someclass.h"
template <class sometype>
sometype testclass<sometype>::blah(sometype newvalue){
value = newvalue;
printf("%i\n", (int)value);
return value;
}
</SNIP>
Here's someclass.h:
<SNIP>
#ifndef SOMECLASS_H
#define SOMECLASS_H
template <class sometype>
class testclass{
public:
sometype value;
sometype blah(sometype newvalue);
};
#endif //ifndef SOMECLASS_H
</SNIP>
and then here's main.cpp:
<SNIP>
#include "someclass.h"
int main(void){
testclass<int> bob;
bob.blah(7);
bob.blah(8);
return 0;
}
</SNIP>
When I try to "make" that one, I get the following linker errors:
<SNIP>
$ make
g++ -Wall main.cpp someclass.cpp -o test
/tmp/ccEyCxBU.o(.text+0x1a): In function `main':
: undefined reference to `testclass<int>::blah(int)'
/tmp/ccEyCxBU.o(.text+0x2b): In function `main':
: undefined reference to `testclass<int>::blah(int)'
collect2: ld returned 1 exit status
make: *** [test] Error 1
</SNIP>
Can anyone tell me what I'm doing wrong? I've tried quite a few different
ways of declaring the "blah" function in that header, but none of them
seem to work (I assume that's where the problem lies; it certainly seems
to be).
Thanks in advance!