Solved

template Class .hpp .cpp files

Posted on 2004-08-11
7
1,369 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 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
Technology Partners: 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!

 
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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need some help with Microsoft Visual Studio C++ 2003 5 86
Unable to start eclipse ? 17 173
C++ help/ Toy problem 19 69
Adding items to a C# list incrementally 5 82
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…
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 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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

737 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