By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

Compiler error with class templates - please help me resolve it

P: n/a
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
]

Dec 3 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"KK" <ke*******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Dec 3 '05 #2

P: n/a
KK wrote:
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>
"<cstring>" is not the C++ std::string header. Use:

#include <string>

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
Dec 3 '05 #3

P: n/a
"KK" <ke*******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com
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
-------

#include <iostream>
#include <cstring>
You do realise that <cstring> is for C-style strings and not for
std::string? For std::string, the required header is <string>.
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< std::string >::Init(int tmp);
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;

start.cpp
---------
#include "atr.h"
void start void()
{

atr<> tst;
atr<std::string> tmp2;
tmp2.Init(1);
}

--
John Carson
Dec 3 '05 #4

P: n/a
"Ali Çehreli" <ac******@yahoo.com> wrote in message
news:dm**********@domitilla.aioe.org
"KK" <ke*******@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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.

--
John Carson
Dec 3 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.