Take the 2-minute tour ×

For my CS Data Structures class, I am implementing a Generic Stack using linked list. However, I am getting "Redefinition of .... " errors for all my constructors and functions in my .cpp file. The header file, "Stach.h", was given by the instructor, and at the end she included "Stack.cpp". Everything works fine when I comment out that line, but she wants it there. Anybody ever heard of this?

Thanks guys,


share|improve this question
Including a source file? Pretty nasty... –  mfontanini Mar 10 '12 at 20:56
did she give you any build instructions? –  moooeeeep Mar 10 '12 at 21:00
Yeah, let's see stack.cpp and stack.h. Including a .cpp file in a header file is unusual and if it breaks the build I suspect you and the professor aren't communicating well. –  Carey Gregory Mar 10 '12 at 21:07
Her instructions: Note that classes templates can not be compiled with separate compilation. Here, you must compile Stack.cpp and client code together as follows: 1) use "include" directive to include Stack.cpp at the end of Stack.h 2)"include" Stack.h in client code 3)Compile the client code I'm pretty sure I followed those steps but I keep getting the same error. Thanks again –  David Mar 10 '12 at 21:07

2 Answers 2

up vote 1 down vote accepted

The only time that I have included a .cpp file is when I wanted to put the implementation of a template in a .cpp file so that my editor recognized it as C++ instead of C. Is the implementation of the stack a template?

If it is, then the implementation of the template methods have to be available to compilation units that are using them. You usually use .ipp or .tcc for template implementation files if you are going to implement somewhere other than in the header itself. This is what Boost and a number of other libraries do. If this is the case, then you should not be compiling stack.cpp since the compiler will take care of that when your client or driver program includes the header file.

share|improve this answer
Thanks that was it, I was using xcode so it must have compiled everything together. I ran it using terminal just compiling the main which includes the header file and it worked fine. –  David Mar 10 '12 at 21:15

Your instructor is wrong. You don't include cpp files in header files.

The compiler will process them everywhere the header is included. If you include the header in a file, all functions implemented in the cpp files will be defined for that translation unit. If you include it in multiple places, multiple translation units will have contain the same symbol, and thus result in the linker error you mentioned.

What could work is if you declare the implementations as inline, but it's still pretty nasty.

EDIT: Seeing how the classes are templates, I can think of one problem being that you specialize it to the same type twice. I would remove the cpp file from the compilation, although, if you can, you should actually change the extension to impl or something else. That way, the compiler will not compile it separately, but it will still be available for the files that specialize your class.

share|improve this answer
Consider how templates are implemented ... usually .tcc or .ipp files instead of .cpp files but same concept –  D.Shawley Mar 10 '12 at 21:04
@D.Shawley it's not at all the same concept. Templates generate symbols only when specialized. –  Luchian Grigore Mar 10 '12 at 21:06
I've seen cases where it made sense. For example, suppose you have a function you need to use and it's in ..\foo\foo.cpp, but it can't be built as a .lib for some reason (eg, it builds only as 32-bit and you need 64). The header file foo.h might simply do "#include <..\foo\foo.cpp>" and nothing more. Though such cases are rare, it's a perfectly legit solution. –  Carey Gregory Mar 10 '12 at 21:06
@CareyGregory in those cases, it's a mere hack. I doubt someone still learning should be taught to do things like that. Just because it's written in some production code, doesn't make it okay. I don't think this is ever the solution to anything. –  Luchian Grigore Mar 10 '12 at 21:07
@LuchianGrigore - note that in his comment he stated that they are using templates –  D.Shawley Mar 10 '12 at 21:12

Your Answer


