473,385 Members | 1,312 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.

question on template

Hi:
im building a template but get some weird error msg.
the code:

------- class Index--------
template<class T> class Index
{
public:
T idx;

Index(T i)
{
idx = i;
}
}

------- main() --------------

void main( void )
{
Index<double> f = index( 1.3 );
}

------compiling error msg---------
'type cast': cannot convert from 'double' to 'Index'
im using vc 2002

how come it's trying convert double to Index???
how should i fix this code??

thnx for any help.

--
{ Kelvin@!!! }
Jul 23 '05 #1
6 1099
"Kelvin@!!!" <ch******************@yahoo.com.hk> writes:

void main( void ) NO!

{
Index<double> f = index( 1.3 );
}

Why "index"? Maybe you meant "Index"? But anyway, try this - it compiles
for me.


template<class T> class Index
{
public:
T idx;

Index(T i)
{
idx = i;
};
};
int main()
{
Index<double> f( 1.3 );
}
Jul 23 '05 #2
void main( void )
{
Index<double> f = Index<double>( 1.3 );
}

For template function it works without <>, but for clases does not.
That's happening becouse compiler can not detrmine which parameter type
to use based on arguments in case of class templates.
Kelvin@!!! wrote:
Hi:
im building a template but get some weird error msg.
the code:

------- class Index--------
template<class T> class Index
{
public:
T idx;

Index(T i)
{
idx = i;
}
}

------- main() --------------

void main( void )
{
Index<double> f = index( 1.3 );
}

------compiling error msg---------
'type cast': cannot convert from 'double' to 'Index'
im using vc 2002

how come it's trying convert double to Index???
how should i fix this code??

thnx for any help.

--
{ Kelvin@!!! }


Jul 23 '05 #3

"Tim Love" <tp*@eng.cam.ac.uk> wrote in message
news:ct**********@gemini.csx.cam.ac.uk...
"Kelvin@!!!" <ch******************@yahoo.com.hk> writes:

void main( void )

NO!

{
Index<double> f = index( 1.3 );
}

Why "index"? Maybe you meant "Index"? But anyway, try this - it compiles
for me.


template<class T> class Index
{
public:
T idx;

Index(T i)
{
idx = i;
};
};
int main()
{
Index<double> f( 1.3 );
}

thnx guys...
but here comes another problem when im trying to wirte a sub class

--- code for iPerf -------

class iPerf : public Index<double>
{
public:
iPerf( double i )
{
idx = i;
}
};

----- compiling err msg ------
'Index<T>': no appropriate default constructor with [ T = double ]

definition of Index has not been changed.

---my Q-----
is it legal to inherit like the way i did?

iPerf and some other class are all index but only differs in type. they are
using different type as index. they all share the same actions that an index
may take.
because there are quite a lot differet types of index, i try not to use the
index<double> since it makes my code meanless.
if it's just index<double>, other may not know what kind of index it
actually is.

rather than writing a sub class for each type of index, is there a better
way to do this w/o losing readability ??

thnx
--
{ Kelvin@!!! }
Jul 23 '05 #4
On Thu, 03 Feb 2005 11:16:05 GMT in comp.lang.c++, "Kelvin@!!!"
<ch******************@yahoo.com.hk> wrote,
class iPerf : public Index<double>
{
public:
iPerf( double i )
{
idx = i;
}
};

----- compiling err msg ------
'Index<T>': no appropriate default constructor with [ T = double ]

definition of Index has not been changed.

---my Q-----
is it legal to inherit like the way i did?


The inheritance is OK, but the constructor is not because Index<T> has
no constructor that takes no argument (a default constructor.) You
must indicate the argument to be used to construct the Index base.

public:
iPerf( double i ) : Index(i)
{
This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.6] Should my constructors use "initialization lists" or
"assignment"?" It is always good to check the FAQ before posting.
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
Jul 23 '05 #5
Kelvin@!!! wrote:

template<class T> class Index
{
public:
T idx;

Index(T i)
{
idx = i;
};
};
int main()
{
Index<double> f( 1.3 );
}


thnx guys...
but here comes another problem when im trying to wirte a sub class

--- code for iPerf -------

class iPerf : public Index<double>
{
public:
iPerf( double i )
{
idx = i;
}
};

----- compiling err msg ------
'Index<T>': no appropriate default constructor with [ T = double ]


You can do A, B, or both.

A) Define a default constructor for Index<>.

--or--

B) change iPerf<> so that it explicitly calls Index<>'s conversion
constructor.

If there is a meaningful default value for idx, then I'd go with A&B.
Otherwise I'd go with B. Whichever way is right, it's very likely that
you also want to define a class copy constructor for Index<>. There are
some other potentially troublesome things too. Here's the way I'd write
it IFF there is a meaningful default value for idx. (I switched your
type from double to long):

#include <iostream>
using namespace std;

template < typename T > class Index
{
public:
T idx;
Index() :idx() {}
Index(const Index & that) :idx(that.idx) {}
explicit Index(T that) :idx(that) {}
virtual ~Index(){}
};

class iPerf : public Index< long >
{
public:
iPerf() {}
iPerf(const iPerf & that) :Index< long >(that) {}
explicit iPerf(long that) :Index< long >(that) {}
};

int main()
{
iPerf a(3);
cout << a.idx << endl;
}

You might benefit from looking into the following topics. I put them in
what I think is their order of importance. I think you're in for a lot
of heartache until you master the first two: mastering the rest of them
will just make your programming life easier and easier.

special member functions
slicing
virtual destructor
member initializer list
conversion constructor
automatic conversion
explicit constructor
explicit initialization of fundamental types
Jul 23 '05 #6
Kelvin@!!! wrote:
"Tim Love" <tp*@eng.cam.ac.uk> wrote in message
news:ct**********@gemini.csx.cam.ac.uk...
"Kelvin@!!!" <ch******************@yahoo.com.hk> writes:
void main( void )


NO!
{
Index<double> f = index( 1.3 );
}


Why "index"? Maybe you meant "Index"? But anyway, try this - it compiles
for me.


template<class T> class Index
{
public:
T idx;

Index(T i)
{
idx = i;
};
};
int main()
{
Index<double> f( 1.3 );
}


thnx guys...
but here comes another problem when im trying to wirte a sub class

--- code for iPerf -------

class iPerf : public Index<double>
{
public:
iPerf( double i )
{
idx = i;
}
};

class iPerf : public Index<double>
{
public:
iPerf(double i) : Index<double>(i) { }
};
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
The following is the XML I have to work with. Below is the question <Table0> <CaseID>102114</CaseID> <CaseNumber>1</CaseNumber> <DateOpened>2005-06-14T07:26:00.0000000-05:00</DateOpened>...
7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
1
by: Alfonso Morra | last post by:
if I have a class template declared as ff: (BTW is this a partial specialization? - I think it is) template <typename T1, myenum_1 e1=OK, my_enum_2=NONE> class A { public: A(); virtual...
10
by: Suki | last post by:
Hi, I'm writing a templated class, and i dont want to use the class otherthan for some predetermined types, say, int, double etc. This class has no meaning for typenames other than those few. ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
16
by: Jeroen | last post by:
Hi all, I have a question which is illustrated by the following piece of code: template <class T> class A { T my_value; }; In a list, I'd like to store pointers to objects of class A....
3
by: DerrickH | last post by:
I have a template class with three policies: PolicyA, PolicyB, and PolicyC. The design dilemma I am having is that B and C depend upon A, but I would like my Host class to derive from all three. In...
9
by: Peskov Dmitry | last post by:
It is a very basic question.Surely i got something wrong in my basic understanding. //Contents of file1.cpp using namespace std; #include <iostream> template <typename T> class my_stack;
3
by: stdlib99 | last post by:
Hi, I have a simple question regarding templates and meta programming. I am going to try and work my way through the C++ Template Metaprogramming, a book by David Abrahams and Aleksey...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.