473,729 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Breaking classes down in header files.

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<somet ype>::blah(some type 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<somet ype>::blah(some type 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(.tex t+0x1a): In function `main':
: undefined reference to `testclass<int> ::blah(int)'
/tmp/ccEyCxBU.o(.tex t+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!
Jul 22 '05 #1
2 1629
On Tue, 22 Jun 2004 21:47:54 -0400 in comp.lang.c++, "Jacob"
<ev***@ncf.ca > wrote,
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/

Jul 22 '05 #2
On Wed, 23 Jun 2004 03:32:20 +0000, David Harmon wrote:
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.
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
1420
by: An Ony | last post by:
Hi, I'm just learning c++ with the book from Bjarne Stroustrup and I just type what is in his book into Visual Studio .NET. Is that a good compiler for that or not? I'm beginning to have my doubts... and it needs to be ANSI C++ without the .NET framework. anyway, what's wrong with this: =====================> class Employee { short dept;
45
3608
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
9
6638
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to use the common example, I have a situation where I am implementing many types of Shapes. My current way of thinking is, well, since they are all the same type, let's just put them all in the same file. The include file would be "shapes.h" and it...
3
2567
by: Javi | last post by:
I have some doubts about what is the best method to distribute classes in .cpp and .h files: - Should I use a file per class? or should I group similar classes in one file? - Is it good to put a lot of class declarations in the same .h, and their definitions in different .cpps? - And, to the contrary, many definitions in one cpp and declarations in different .h? - How to deal with precompiled headers? is it ok to include all headers in the...
2
9473
by: Alfonso Morra | last post by:
I have a (largish) "master" header file (>130 loc). I also have a number of seperate "satelite" header files which declare various objects used by the object declared in the "master" header file. In a clean design, these "satelite" objects (i.e. declared in the satellite header files), should really be nested classes within the master file. However, if I was to put all the declarations in one massive file, it will easily exceed 1000...
27
31416
by: The Bicycling Guitarist | last post by:
Hi. I found the following when trying to learn if there is such a thing as a non-breaking hyphen. Apparently Unicode has a ‑ but that is not well-supported, especially in older browsers. Somebody somewhere said: Alternately, you can use CSS to declare a class having: ..nowrap { white-space:nowrap } .... and then wrap the compound word in a <span class=nowrap></span> tag (or any other suitable inline tag). You can also try {...
18
2042
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise. Yet, in my understanding, attributes are only __gc and __value class specific and do not apply to __nogc classes. Is this correct ? If so, how is the packing alignment of __nogc classes stored ?
150
6545
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and see if you agree with my opinion or not. Tony Marston http://www.tonymarston.net
1
2794
by: Swapnil Kale | last post by:
Hi, I'm working on a Migration project (Forte to JAVA). The forte client had a C++ dll which used to call one more FORTE dll for a complex database calculations. Now all the forte code has been migrated to JAVA except this piece of code where C++ dll calls Forte DLL.
1
9202
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2683
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2165
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.