473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Compiler error with class templates - please help me resolve it

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
4 1932
"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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: SC Shin | last post by:
Hi, everyone I've got problem. I am able to compile following code without error on Compaq, Linux machine But I am not able to compile this with error on Sun with forte7 compiler: line 26:...
2
by: Mike Fisher | last post by:
I'm seeing an error when I try to run/debug a web service. Although it doesn't happen every time, it does occur more than half of the times I hit F5. It appears to be returned by the the JIT...
5
by: Shlomi | last post by:
Hi ! I've wrote quite a big code which bases on a considerable large amount of template classes. (uses many different kinds of each template class). Now, without prior knowledge of mine, it...
14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
2
by: Itjalve | last post by:
This gives me a fatal error. I'm using .NET VC7.1 and made a win32 consol app, I have no problems with VC6. Debug build. I have removed nearly all my code this is whats left. From the beginning...
7
by: p | last post by:
WE had a Crystal 8 WebApp using vs 2002 which we upgraded to VS2003. I also have Crystal 9 pro on my development machine. The web app runs fine on my dev machine but am having problems deploying....
3
by: manoj.pattanaik | last post by:
Hi, I am trying to compile following piece of code (bb.cpp) using aCC (HP ANSI C++ B3910B A.03.37) compiler on HP-UX 11.23. It gives error:485 //bb.cpp -- Starts #include <iostream> using...
7
by: Chameleon | last post by:
This code below compiles fine with VS.2005 but with gcc 3.4.2 not. ------------------ template<class T> static void Wastage1D::clever_erase(vector<T> &v, vector<typename vector<T>::iterator>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.