Compiler error with class templates - please help me resolve it

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
]
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

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. [...]
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);

What do you expect to happen when you initialize a string with an int? That
operation is not defined.

[...]
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

Makes sense to me so far. What are you trying to do?

Ali
 
L

Larry I Smith

KK said:
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

"<cstring>" is not the C++ std::string header. Use:

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
]

See comment above...

Larry
 
J

John Carson

KK said:
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.

The basic problem is that the specialisation is not declared in the header
file, so the compiler cannot find it. You are correct that the definition of
the specialisation should be in a .cpp file, but its declaration needs to be
in the header file. There are some other problems as well, which I comment
on below.

Thanks.
-KK

atr.h

You do realise that <cstring> is for C-style strings and not for
std::string? For std::string said:
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);
}

Add

template<>
void atr said:
atr.cpp
---------

#include "atr.h"
#include <cstring>
void atr< std::string >::Init(int tmp)
{
std::string str;
_val = T(str);
}

Omission of template<> before this specialisation is non-standard. Note too
that T is undefined. Presumably the code should be

_val = str;
 
J

John Carson

Ali Çehreli said:
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. [...]
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);

What do you expect to happen when you initialize a string with an
int? That operation is not defined.

He doesn't. He initialises a T with an int. That operation is defined for
some T.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top