Breaking classes down in header files.

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!
 
D

David Harmon

On Tue, 22 Jun 2004 21:47:54 -0400 in comp.lang.c++, "Jacob"
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.

The fix is simple: all your templates belong in the header file.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[34.12] Why can't I separate the definition of my templates class from
it's declaration and put it inside a .cpp file?" It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
J

Jacob

The fix is simple: all your templates belong in the header file.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[34.12] Why can't I separate the definition of my templates class from
it's declaration and put it inside a .cpp file?" It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Works beautifully, thanks. I had looked through the FAQ, but obviously I
wasn't thorough enough.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top