templates

  • Thread starter Rene Ivon Shamberger
  • Start date
R

Rene Ivon Shamberger

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
 
V

Victor Bazarov

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
 
R

Rene Ivon Shamberger

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 ==========
 
B

Bart van Ingen Schenau

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top