Solved

template Class .hpp .cpp files

Posted on 2004-08-11
7
1,362 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
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

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

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.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

770 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