Solved

Templates declarations in header files?

Posted on 1998-05-04
7
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

630 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