473,386 Members | 1,610 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,386 software developers and data experts.

type conversion w/ template

I current have
template<typename T>
class Vector
{
....
Vector(const Vector& Vin)
{
....
}
...
}
int main(void)
{
Vector<double> V(5);
Vector<int> V1(V);//doesnt work
}

How would I solve this problem?
Thanx.

Jul 23 '05 #1
2 1009
bl**********@gmail.com wrote:
I current have
template<typename T>
class Vector
{
...
Vector(const Vector& Vin)
{
...
}
..
}
int main(void)
{
Vector<double> V(5);
Vector<int> V1(V);//doesnt work
}

How would I solve this problem?


Take a look at constructing from a range of iterators.
The std::vector does it, so could your Vector. Essentially,
you need to define a constructor that would take two arguments,
and construct your vector (sorry, Vector) from them. Make that
constructor a member template:

template<typename T>
class Vector
{
...
template<typename Iterator> Vector(Iterator i1, Iterator i2)
{
storage = ???[std::distance(i1, i2)];
while (i1 != i2)
*storage++ = *it1++;
}
...

I know that it's not necessarily how you implemented your class
(ahem, template), but it should give you the idea.

V
Jul 23 '05 #2
Ian
bl**********@gmail.com wrote:
I current have
template<typename T>
class Vector
{
...
Vector(const Vector& Vin)


This is shorthand for Vector(const Vector<T>& Vin), you can't use a
Vector<double> as it is a completely different class.

You could try a template constructor:

template <typename Other> Vector(const Vector<Other>& Vin).

This assumes Other can be converted to a T somehow, that bit's up to you.

Ian
Jul 23 '05 #3

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

Similar topics

1
by: Eric | last post by:
Say you have the following: template <typename _T> struct A { typedef _T T; A (T t); operator T () const; }; How do you define the T conversion operator?
16
by: frs | last post by:
See example below: Why does the output of 'a' work and the output of 'b' fails to compile? Is there a way to write class 'something' so that 'b' converts correctly by default? (include iostream,...
4
by: Frank-René Schäfer | last post by:
-- A class needs to have N members according to N types mentioned in a typelist (possibly with one type occuring more than once). -- The classes should be generated **avoiding** multiple...
3
by: mario | last post by:
Hi! First of all, sorry for my English, it's not my native tongue. Anyway, here we go: suppose I create a class that handles my own custom text strings. No, suppose I create TWO such classes,...
5
by: Preben | last post by:
Hi, I get this error when trying to compile: -------- # g++ -c KGreyImage.cpp KGreyImage.cpp:25: error: expected constructor, destructor, or type conversion before '*' token --------
15
by: shuisheng | last post by:
Dear All, Assume I have a class named Obj. class Obj { }; And a class named Shape which is derived from Obj. class Shape: public Obj
3
by: Szabolcs | last post by:
Consider the attached example. When I try to compile it (with g++ 4.1.2), I get the error message error: no matching function for call to 'fun(A<int>&)' However, if I do not use templates, it...
2
by: algatt | last post by:
Hello, I am trying to compile the TPIE files but there is a file that's constantly giving errors about the templates. I am using gcc 3.4.5 on Eclipse using Windows XP. The following is the code of...
5
by: Fei Liu | last post by:
Hello, I just hit a strange problem regarding SFINAE. The following code causes compile error (void cannot be array element type), I thought SFINA should match test(...) version instead and not...
4
by: danilo.turina | last post by:
Hi all, today I encountered a problem that I'm only able to solve by using reinterpret_cast (and I'd like to avoid it). This was my code until yesterday (omitting includes, "using namespace"...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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.