Connecting Tech Pros Worldwide Help | Site Map

Breaking classes down in header files.

  #1  
Old July 22nd, 2005, 03:06 PM
Jacob
Guest
 
Posts: n/a
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!
  #2  
Old July 22nd, 2005, 03:06 PM
David Harmon
Guest
 
Posts: n/a

re: Breaking classes down in header files.


On Tue, 22 Jun 2004 21:47:54 -0400 in comp.lang.c++, "Jacob"
<ev910@ncf.ca> wrote,[color=blue]
>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.[/color]

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/

  #3  
Old July 22nd, 2005, 04:15 PM
Jacob
Guest
 
Posts: n/a

re: Breaking classes down in header files.


On Wed, 23 Jun 2004 03:32:20 +0000, David Harmon wrote:
[color=blue]
> 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/[/color]

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse a file in C++ AdrianH insights 5 March 2nd, 2008 03:39 AM
strings and input files Marco Trapanese answers 2 April 24th, 2007 12:55 PM
regarding "goto" in C M.B answers 77 January 13th, 2006 06:05 AM
Please Stop It Rebuilding Everything! Andy Capon answers 8 November 17th, 2005 03:19 PM