Solved

template Class .hpp .cpp files

Posted on 2004-08-11
7
1,365 Views
Last Modified: 2012-06-27
I am having trouble separating a templated class into .hpp for the declaration and .cpp for the definition.  Are you not allowed to do this?  My code works when I have it all in the .hpp with nothing but the #include "Fifo.hpp" in the .cpp file.  It will not compile if the definition is in the .cpp file.  Any advice?
0
Comment
Question by:klopex
  • 3
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 80 total points
ID: 11776293
>>It will not compile if the definition is in the .cpp file.  Any advice?

Even though the C++ standard allows the separation of template declaration and implementation, most compilers (technically speaking: all compilers) do not support that. As a workaround: Add

#include "fifo.cpp"

to the end of "fifo.hpp", e.g.

#ifndef FIFO_HPP // header guard
#define FIFO_HPP

// declarations go here
// ...
// ...

#include "fifo.cpp"

#endif // header guard
0
 
LVL 3

Author Comment

by:klopex
ID: 11776420
I had a #include "Fifo.hpp" at the top of the .cpp file, but it wouldn't work...

How would including the .cpp at the bottom be different?

If I do this, I guess I will need a "header guard" of sorts for the .cpp file as well, yes?
0
 
LVL 86

Expert Comment

by:jkr
ID: 11776486
>>How would including the .cpp at the bottom be different?

Because in all files where you include 'fifo.hpp', the .cpp file will be pulled i also, not the other way round.

>>If I do this, I guess I will need a "header guard" of sorts for the .cpp file as well, yes?

No, these only make sense for .hpp/.h files to prevent "symbol xyz multiply defined"  error messages when such a file is included more than once in a project.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 3

Author Comment

by:klopex
ID: 11776858
I have done what you have suggested, and now I am getting the following errors when I compile:

$ make
g++ -c Fifo.cpp
Fifo.cpp:10: redefinition of `bool Fifo<T>::write(T*, unsigned int)'
Fifo.cpp:10: `bool Fifo<T>::write(T*, unsigned int)' previously declared here
Fifo.cpp:10: no `bool Fifo<T>::write(T*, unsigned int)' member function
   declared in class `Fifo<T>'


Are you sure I should not be using a "header guard"?
0
 
LVL 86

Expert Comment

by:jkr
ID: 11776978
It should be 'guarded' by the one in the .hpp file already...
0
 
LVL 3

Author Comment

by:klopex
ID: 11777015
It wasn't.  When I compile the Fifo.cpp class, it includes the .hpp which includes the .cpp file.  .cpp is present twice when compiling the .cpp file.  Am I supposed to never do that?

By the way, I put in the guard and it compiles fine both ways.

Thanks for the help.
0
 
LVL 4

Assisted Solution

by:AssafLavie
AssafLavie earned 20 total points
ID: 11777020
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

791 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