473,685 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(1 7) : 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\includ e\xmemory(136) : while compiling class-template member
function 'void atr<T>::Init(in t)'
with
[
T=std::string
]
c:\work\start.c pp(57) : see reference to class template instantiation
'atr<T>' being compiled
with
[
T=std::string
]

Dec 3 '05 #1
4 1953
"KK" <ke*******@yaho o.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(1 7) : 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(1 7) : 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\includ e\xmemory(136) : while compiling class-template member
function 'void atr<T>::Init(in t)'
with
[
T=std::string
]
c:\work\start.c pp(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*******@yaho o.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.ai oe.org
"KK" <ke*******@yaho o.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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
2317
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: Unexpected type name: Apple Please carefully review following code And Let me know what problem is and how to solve this problem
2
17556
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 compiler as the page is requested by the browser. The result is that the "compiler failed with error code 2000". I have tested the same code on another workstation with VS.NET and it works fine EVERY time. I'm convinced it's not the code and I...
5
1482
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 turns out that this code should be compiled with compiler which doesn't support templates ! (OUCH!)
14
4328
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 intervention such as adding type declarations. It uses rather advanced static type inference techniques to deduce type information by itself. In addition, it determines whether deduced types may be parameterized, and if so, it generates...
6
4744
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 appreciated. Thanks in advance
2
2950
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 the template was defined in a .h file, but that has no effect, same error. I have seen when searching for fatal error that there are some problems with templates and optimization, but nothing as simple like this. I'm i doing something wrong?
7
5008
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. I created the websetup and built the MSI, have the bundled version. Copied to webserver and ran Websetup.msi. Said I had to remove old version, which I did, then reran WebSetup.msi and keeps giving me this error. "The installer was interrupted...
3
2916
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 namespace std; class abc;
7
4340
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> &its, vector<T> &vo) { ........ } ------------------ I run gcc like this: gcc -c wastage1d.cpp
0
8512
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8928
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8774
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7591
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6436
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4303
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4525
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
1931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.