Templated function being reported as “undefined reference” during compilation?

A way to solve this problem is to a. Remove '#include "c. Hpp"' from c.

Cpp AND b. Include 'c. Cpp' at the end of 'c.

Hpp' (strange sounding '#include "c. Pp"') This way the template definitions are availabe to each translation unit that includes 'c. Hpp' without explicitly doing so in each .

Cpp file. This is called the inclusion model.

A way to solve this problem is to a. Remove '#include "c. Hpp"' from c.

Cpp AND b. Include 'c. Cpp' at the end of 'c.

Hpp' (strange sounding '#include "c. Pp"') This way the template definitions are availabe to each translation unit that includes 'c. Hpp' without explicitly doing so in each .

Cpp file. This is called the 'inclusion model.

It also causes trouble if you evolve a need for things in c. Cpp that shouldn't be included in each translation unit. – Tony Delroy Oct 7 '10 at 5:55 @Tony: Oh, so what do you do then?

– Chubsdad Oct 7 '10 at 6:01 Then you really do have to move the template implementation into the header. Just thinking of the One Definition Rule and all that jazz.... – Tony Delroy Oct 7 '10 at 6:38 @Tony:Ok, and then explicitly specialize the templates in the header file right? – Chubsdad Oct 7 '10 at 6:59 1 Yes, all template implementation must be visible at the points of use, so if the specialisations are used by other cpps, then the header is the best place to share them (assuming we've crossed the point of abandoning the inclusion model you started with).

Even for private templates, it would be pretty confusing not to localise specialisations with general implementations so I'd say they all out to be together, whether in the . H or .cpp. – Tony Delroy Oct 7 '10 at 7:04.

I believe this is because when you compile c. Cpp the compiler doesn't know it needs to generate code for C::call(Merc&), and when you're compiling main. Cpp it doesn't have the definition of C::call(T&) from which to instantiate C::call(Merc&).

Template definitions are essentially semantic macros, so much like the preprocessor's #define lexical macros they must be visible in the compilation units that use them.

Essentially, you cannot have related template declarations and definitions in separate files in current C++. You should fully define class C in one file.

Just by way of clarification: that's true for the common and illustrated case of public/protected templates, but not necessary for private templates. – Tony Delroy Oct 7 '10 at 5:57 what happens with friend classes and private templates? – nakiya Oct 7 '10 at 6:06.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions