Solved

Templates declarations in header files?

Posted on 1998-05-04
7
179 Views
Last Modified: 2010-04-10
Is it legal to have only a template declaration in a header file if the template is defined somewhere in a regular file?  That is,

For example, can I just include the header file "SomeClass.h" into the "SomeProgram.cpp" as long as the file "SomeClass.cpp" is compiled and linked into the program (but not included into "SomeProgram.cpp").  

****************SomeClass.h**************
template <class T> class SomeClass
{
public:
   int x;
   SomeClass(int i);  // Declared, not defined.
};
****************SomeClass.cpp**************
// Define constructor.
template <class T> SomeClass::SomeClass(int i);
{
  x = i;
}
****************SomeProgram.cpp**************
#include "someclass.h"
  SomeClass SC(5);

A EE client was trying to do this in a program and I didn't think it was allowed,  I just want to make sure.  It caused linker errors, which seems to make sense as things compiled, but the necessary functions weren't there.
0
Comment
Question by:nietod
7 Comments
 
LVL 7

Expert Comment

by:galkin
ID: 1172021
Actually template class public functions must be declared inline.
0
 
LVL 5

Expert Comment

by:yonat
ID: 1172022
Yes, this is allowed according to the new C++ standard. It is called "separate template compilation". However, only a few compiler support it, and the ones I use are not among them :-(

See http://www.ses.com/~clarke/cpptips/templ_sep_comp_spec for a short explanation.
0
 
LVL 22

Author Comment

by:nietod
ID: 1172023
Sorry Galkin, but it looks like you are out of date.

Yonat, If you answer the question I'll give you the points.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:danny_pav
ID: 1172024
I am not sure but I think that you have to use the extern keyword on the function bodies when they are not inline.
Danny Pav
0
 
LVL 22

Author Comment

by:nietod
ID: 1172025
Yes, that is correct.  Check the web site that yonat posted for details.
0
 
LVL 5

Accepted Solution

by:
yonat earned 20 total points
ID: 1172026
Thanks.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1172027
Gosh .. this C++ language thingy sure does get changed more than a baby on laxatives !!

It certainly keeps us poor experts on our toes .. and keeps those not-so-poor people that write compilers in jobs for a long time.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now