473,698 Members | 2,445 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 1624
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
1415
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
3601
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
6637
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
2561
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
9468
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
31412
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
2041
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
6525
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
2792
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.
0
8675
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8862
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...
0
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
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
3
2002
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.