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

question on template

P: n/a
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
Share this Question
Share on Google+
6 Replies


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

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

P: n/a

"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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.