R
Rene Ivon Shamberger
Oops, I posted this question before, but in the wrong place :-o
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 ==========
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 ==========