template Class .hpp .cpp files

Posted on 2004-08-11
Medium Priority
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?
Question by:klopex
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
  • 3
  • 3
LVL 86

Accepted Solution

jkr earned 320 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

Author Comment

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?
LVL 86

Expert Comment

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.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

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"?
LVL 86

Expert Comment

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

Author Comment

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.

Assisted Solution

AssafLavie earned 80 total points
ID: 11777020

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
Suggested Courses

762 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