Solved

Templates declarations in header files?

Posted on 1998-05-04
7
180 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 103
How to convert MFC APP to Win32 APP. 19 82
C++ :Change value from  DisableCMD registry 4 60
draw a Christmas tree by using a nested loop? 26 63
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

831 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