template question

Hi , from some previous thread i tried to construct a clas..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template<class T>
class myvector
{
	private : 
		T a;
	public:
		template<class U>
		myvector<T> operator * (U u);
		T getA() { return a; }
};
template<class T> myvector<T> myvector<T>::operator*(U u)
{
	return a * u.a;
};


but gives me error of "U undefined "
I dont understand why this error .
Thanks in advance
1
2
3
4
5
6
template<class T> 
template<class U> //the method is a template
myvector<T> myvector<T>::operator*(U u)
{
	return a * u.a;
};
I will let you figure out the other errors
Thanks ne555 ,
but , Implementing the code further
getting the error error C2512: 'myvector' : no appropriate default constructor available
please advice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
using namespace std;
template<class T>
class myvector
{
	private : 
		T a;
	public:
		myvector() { }
		myvector(T b) { a = b; } 
		template<class U>
		myvector<T> operator * (U u);
		T getA() { return a; }
};
template<class T>
template<class U>
myvector<T> myvector<T>::operator*(U u)
{
	return a * u;
};
template<class T>
myvector<T> myvector<T>::operator=(const myvector<T> &rhs)
{
	myvector<T> temp; 
	temp.a = rhs.a;
	return temp;
};


int _tmain(int argc, _TCHAR* argv[])
{
	myvector temp(5.5);
	myvector result = temp * 2.0;
	

	return 0;
}
Last edited on
>> myvector<T> operator * (U u);
>> T getA() { return a; }
In this line you should return myvector< T > type.
But you return typename T.
Last edited on
myvector is a class template so your line 32 and 33 are wrong, you need to specify the parameter type.
And please, use constructor list in line 10(myvector(T b) : a(b)) insteadd of affectation
thanks aquaz and Ivan Sidarau it worked .
Topic archived. No new replies allowed.