Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > templates

Reply
Thread Tools

templates

 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      12-05-2012
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      12-05-2012
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
 
Reply With Quote
 
 
 
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      12-05-2012
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 ==========
 
Reply With Quote
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      12-05-2012
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
 
Reply With Quote
 
Rene Ivon Shamberger
Guest
Posts: n/a
 
      12-05-2012
Outstanding Bart!
Thanks so much, it works like a charm.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Monster Templates - Question about Submitting Templates Fred HTML 1 09-26-2005 01:09 AM
Templates within templates Tom McCallum C++ 2 08-04-2004 04:44 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
using templates in templates John Harrison C++ 8 07-31-2003 12:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57