Convert class type int to double

Jun 14, 2010 at 7:08am
Hello guys,
I am a bit stuck here :
My class vector has x,y,z and these values can be double or int .
I have other functions that add two vectors together which works fine if i am passing two vectors of the same type . What i am trying to solve here is passing one vector as double and the other as int .
I thought Operator overload would do it but it didnt work.
I hope someone can suggest something .
Thanks
Jun 14, 2010 at 7:54am
Are you doing it with templates?
It's simple that way. Then your operator + should look like this:
1
2
3
4
5
6
7
8
template<typename A, typename B>
vec<A> operator + (vec<A> a, vec<B> b){
	vec<A> c;
	c.x = a.x + b.x;
	c.y = a.y + b.y;
	c.z = a.z + b.z;
	return c;
}
You just have to note that there two typenames.

If, for some reason, you're not using templates you could write a function to convert vector of ints to vector of doubles:
vec_double(const vec_int& v) : x(v.x), y(v.y), z(v.z){}
This is just like a copy constructor. Now the function you wrote to add vec_double will work for vec_int too.

The last way would be to actually write four functions ( int + int, int + double, double + int and double + double ) but that's what templates are for.
Jun 14, 2010 at 1:35pm
The above is ok, but it requires that B be convertible to A, or more specifically, in the
expression

a + b

where a and b are vectors, that the right-hand side's contained type be convertible
to the left-hand side's contained type.

It also somewhat breaks the commutative rule in that a + b != b + a.

To solve both of the above problems, you need a bit more template machinery that
can pick the correct type of the resulting vector given two contained types.

My guess though is that all of this is well beyond what you're looking for.
Jun 14, 2010 at 2:07pm
i cant do this because i have a function called plus where it adds each component of vector
1
2
3
4
5
vector<T> vector<T>::plus(const vector<T>& vec)
{
     vector<T> result(x+vec.x,y+vec.y,z+vec.z); 
	 return result;
}

but this gives error if i pass one as double one as int
Last edited on Jun 14, 2010 at 2:07pm
Jun 14, 2010 at 7:30pm
Your problem is that you have only one type T int your template. Add another one and it should work:
1
2
template<typename T, typename S>
vector<T> vector<T>::plus(const vector<S>& vec){/*...*/}
Jun 15, 2010 at 12:27pm
I tried doing it with two templates but i cant get it compiled i keep getting errors :
this is my class :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class T>
class vector
{
public:
	vector(){x=0;y=0;z=0;};
    vector(T X1,T X2,T X3){x=X1;y=X2;z=X3;};
    friend ostream& operator<< <> (ostream& output,vector<T>& );
	//T operator+(const T &);
	vector<T> operator -();
	template <class T,class S>
    vector<T> plus(const vector<S>& vec);
private:
    T x,y,z;
};


this is my plus function:
1
2
3
4
5
6
template<class T, class S>
vector<T> vector<T>::plus(const vector<S>& vec)
{
     vector<T> result(x+vec.x,y+vec.y,z+vec.z); 
	 return result;
}
Jun 15, 2010 at 2:30pm
Can you post the compile errors?

--Rollie
Jun 15, 2010 at 2:54pm
I dont this this is how it should be , but heres the error:
1>c:\users\elkhas\documents\visual studio 2010\projects\final\final\vectror.h(41): error C2244: 'vector<T>::plus' : unable to match function
Jun 15, 2010 at 3:14pm
ok i got it to work now with two parameters T and U
This is how it should be:
template<class T>
template<class U>
vector<T> vector<T>::plus(const vector<U>& vec)
{
vector<T> result(x+vec.x,y+vec.y,z+vec.z);
return result;
}
This article helped :http://www.cplusplus.com/forum/general/19017/

But i am still getting error when i pass vector<int> a(1,2,3); and vector<double> b(1,2,3) to the plus function it tells me i cant add double to int which is what i am trying to solve.
Jun 15, 2010 at 10:31pm
See my original post.

if T == int and U == double, then yes, you cannot add an int and a double and store the result in an int.

if T == double and U == int, then it should compile.

So given

1
2
vector<int> a;
vector<double> b;


then

a + b does not compile, but b + a does.


Jun 15, 2010 at 11:37pm
ooh i see so you telling me i always have to return a double in my functions or theres a better way to overcome this problem.
Jun 15, 2010 at 11:47pm
I'm saying that unless you are using a compiler that supports C++0x's decltype keyword, then
you have a big problem. Essentially, given T and U, you have to implement the compiler's promotion
rules and determine whether the result of the expression should be a vector<T> or a vector<U>.
But that will require a _LOT_ of template machinery.
Topic archived. No new replies allowed.