K
KK
Hello all,
I have class 'atr' which is based on templates. My idea was to
initialize it two scenarios
1. std::string case
2. other data types
I have defined the Init for the above different cases accordingly (code
shown below). However, I get compiler error. Please help me resolve
it.
Thanks.
-KK
atr.h
-------
#include <iostream>
#include <cstring>
template <class T = unsigned int>
class atr
{
T _val;
public:
atr(void) {};
~atr(void){};
void Init( int tmp);
};
template <class T >
void atr< T >::Init(int tmp)
{
_val = T(tmp);
}
atr.cpp
---------
#include "atr.h"
#include <cstring>
void atr< std::string >::Init(int tmp)
{
std::string str;
_val = T(str);
}
start.cpp
---------
#include "atr.h"
void start void()
{
atr<> tst;
atr<std::string> tmp2;
tmp2.Init(1);
}
c:\work\atr.h(17) : error C2440: 'type cast' : cannot convert from
'int' to 'std::string' ( /* corresponding to line "_val = T(tmp);" */ )
No constructor could take the source type, or constructor overload
resolution was ambiguous
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xmemory(136) : while compiling class-template member
function 'void atr<T>::Init(int)'
with
[
T=std::string
]
c:\work\start.cpp(57) : see reference to class template instantiation
'atr<T>' being compiled
with
[
T=std::string
]
I have class 'atr' which is based on templates. My idea was to
initialize it two scenarios
1. std::string case
2. other data types
I have defined the Init for the above different cases accordingly (code
shown below). However, I get compiler error. Please help me resolve
it.
Thanks.
-KK
atr.h
-------
#include <iostream>
#include <cstring>
template <class T = unsigned int>
class atr
{
T _val;
public:
atr(void) {};
~atr(void){};
void Init( int tmp);
};
template <class T >
void atr< T >::Init(int tmp)
{
_val = T(tmp);
}
atr.cpp
---------
#include "atr.h"
#include <cstring>
void atr< std::string >::Init(int tmp)
{
std::string str;
_val = T(str);
}
start.cpp
---------
#include "atr.h"
void start void()
{
atr<> tst;
atr<std::string> tmp2;
tmp2.Init(1);
}
c:\work\atr.h(17) : error C2440: 'type cast' : cannot convert from
'int' to 'std::string' ( /* corresponding to line "_val = T(tmp);" */ )
No constructor could take the source type, or constructor overload
resolution was ambiguous
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\xmemory(136) : while compiling class-template member
function 'void atr<T>::Init(int)'
with
[
T=std::string
]
c:\work\start.cpp(57) : see reference to class template instantiation
'atr<T>' being compiled
with
[
T=std::string
]