Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   templates (http://www.velocityreviews.com/forums/t955143-templates.html)

Rene Ivon Shamberger 12-05-2012 01:34 PM

templates
 
As I would normally '#include' the header files to a main.cpp I have noticed that I need to '#include' the header and source file when I there is a template in those files. Is this the normal for C++?

Example of normal include
// with out a function template
#include "my_file.hpp"

void main(){}


Example of abnormal include
//With function template
#include "my_file.hpp"
#include "my_file.cpp"
void main(){}

TIA
TIA

Victor Bazarov 12-05-2012 01:46 PM

Re: templates
 
On 12/5/2012 8:34 AM, Rene Ivon Shamberger wrote:
> As I would normally '#include' the header files to a main.cpp I have

noticed that I need to '#include' the header and source file when I
there is a template in those files. Is this the normal for C++?

You #include what you want your compiler to see.

>
> Example of normal include
> // with out a function template
> #include "my_file.hpp"
>
> void main(){}


int main(){}

>
>
> Example of abnormal include
> //With function template
> #include "my_file.hpp"
> #include "my_file.cpp"
> void main(){}


Also, see the FAQ.

V
--
I do not respond to top-posted replies, please don't ask

Rene Ivon Shamberger 12-05-2012 02:51 PM

Re: templates
 
I have double checked this method, and it is fine. However, there is a problem at linker time. Perhaps one of you can do me the honours to check out and let me know what I am doing wrong? Thanks!!

---- myclass.hpp -----
#ifndef ABC_MYCLASS_HPP
#define ABC_MYCLASS_HPP

#include <string>

namespace ABC{
class myClass{
private:
std::string str;
public:
MyClass(){str.clear();}
virtual ~MyClass(){}
template< typename T> std::string& format( const T& data );

};//class
}// name space
#endif

---- myclass.cpp ----
#ifndef ABC_MYCLASS_HPP
#include "myclass.hpp"
#endif
template< typename T>
std::string& ABC::MyClass::format(const T& data){
std::stringstream num;
num.flush();
num << data;
str = num.str();
return str;
}

---- main.cpp -----
#include <iostream>
#include "myclass.hpp"
int main(){
ABC::MyClass mc;
int i = 100;
std::string text = mc.format(i); //<<-- linker error
....

return 0;
}


1>------ Build started: Project: ABC, Configuration: Release Win32 ------
1> main.cpp
1>main.obj : error LNK2001: unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall ABC::MyClass::format<int>(int const &)" (??$format@H@MyClass@ABC@@QAEAAV?$basic_string@DU? $char_traits@D@std@@V?$allocator@D@2@@std@@ABH@Z)
1>{myfolder}\Release\ABC.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Bart van Ingen Schenau 12-05-2012 03:10 PM

Re: templates
 
On Wed, 05 Dec 2012 06:51:33 -0800, Rene Ivon Shamberger wrote:

> I have double checked this method, and it is fine. However, there is a
> problem at linker time. Perhaps one of you can do me the honours to
> check out and let me know what I am doing wrong? Thanks!!
>
> ---- myclass.hpp -----
> #ifndef ABC_MYCLASS_HPP
> #define ABC_MYCLASS_HPP
>
> #include <string>
>
> namespace ABC{
> class myClass{
> private:
> std::string str;
> public:
> MyClass(){str.clear();}
> virtual ~MyClass(){}
> template< typename T> std::string& format( const T& data );
>
> };//class
> }// name space
> #endif
>
> ---- myclass.cpp ----
> #ifndef ABC_MYCLASS_HPP
> #include "myclass.hpp"
> #endif
> template< typename T>
> std::string& ABC::MyClass::format(const T& data){
> std::stringstream num;
> num.flush();
> num << data;
> str = num.str();
> return str;
> }
>

Separate compilation of templates is not generally supported by compilers.
The problem is that the compiler must be able to see in which context a
template is being used to know which specializations to generate. This is
not possible with separate compilation, because each source file is
processed independently from the others, so the information about which
specializations are needed is not available when processing myclass.cpp

The typical solutions are
1. Place all template code in the header
2. Place the template code in a separate file (often with a .icc or .tcc
extension) and #include that file at the end of the header.

Bart v Ingen Schenau

Rene Ivon Shamberger 12-05-2012 09:09 PM

Re: templates
 
Outstanding Bart!
Thanks so much, it works like a charm.



All times are GMT. The time now is 03:56 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.