Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

template Class .hpp .cpp files

Posted on 2004-08-11
7
Medium Priority
?
1,380 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
[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
  • 3
  • 3
7 Comments
 
LVL 86

Accepted Solution

by:
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
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 80 total points
ID: 11777020
0

Featured Post

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.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

636 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