question on template

K

Kelvin@!!!

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.
 
T

Tim Love

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 );
}
 
I

ishijak

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.
 
K

Kelvin@!!!

Tim Love said:
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
 
R

red floyd

Kelvin@!!! said:
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) { }
};
 
D

David Harmon

On Thu, 03 Feb 2005 11:16:05 GMT in comp.lang.c++, "Kelvin@!!!"
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/
 
H

Howard Gardner

Kelvin@!!! said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,048
Members
47,647
Latest member
NelleMacy9

Latest Threads

Top