Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Making class a template

Hi,

I created a class that stores 'double' numbers in a vector with two methods - minimum() and sort(). I also overloaded >> operator(simplified version). I need to create a template of this class in order to have ints, doubles and so on. I think i better write both classes below to make it clear.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Samplet {

public:
Samplet(vector<double> vec)
: v(vec) {}
Samplet() {}

friend istream& operator >>(istream& is, Samplet& s);

void sort_data();
double minimum();

private:
vector <double> v;
int size;
};

double Samplet::minimum() {
sort_data();
double minVal = v.front();
return minVal;
}

void Samplet::sort_data() {
sort(v.begin(), v.end());
}

//Input stream overloading for Samplet objects.
istream& operator >>(istream& is, Samplet& s) {

vector<double> vec;
double x;
while(is >> x)
vec.push_back(x);
s = Samplet(vec);
return is;
}

int main() {
Samplet s;
cin >> s;
cout << s.minimum() << endl;
}

//This is a template version

#include <iostream>
#include <vector>
#include <string>

using namespace std;

template <typename T>
class Samplet {

public:
Samplet(vector<T> vec)
: v(vec) {
}

Samplet() {}

friend istream& operator >>(istream& is, Samplet<T>& s);

void sort_data();
T minimum();

private:
vector <T> v;
int size;
};

template <typename T>
T Samplet<T>::minimum() {
sort_data();
T minVal = v.front();
return minVal;
}

template <typename T>
void Samplet<T>::sort_data() {
sort(v.begin(), v.end());
}

//Input stream overloading for Samplet objects.
template <typename T>
istream& operator >>(istream& is, Samplet<T>& s) {

vector<T> vec;
T x;
while(is >> x)
vec.push_back(x);
s = Samplet <T>(vec);

return is;
}

int main() {

Samplet s;
cin >> s;
cout << s.minimum() << endl;
}

Now, the non-template class works fine. But when i am trying to compile a template version it would not.
I have tried several exercises on templates and they work fine and I think my problem lies with my overloaded operator>>. (I changed the code in operator overloaded method several times, but still no luck.

Does anyone know what am i doing wrong - am I missing here something?

Any help will be appriciated.

Alex
[2984 byte] By [alexteslin] at [2007-11-11 8:02:53]
# 1 Re: Making class a template
Hi,
I had a problem with tat a few ago myself. Have a read though this thread
http://forums.dev-archive.com/showthread.php?t=150243
VS6might be your problem...
Cheers,

D
drkybelk at 2007-11-11 21:01:44 >
# 2 Re: Making class a template
Alex,

Yes, we had a simlar thread about this a few weeks ago. But what exactly is the problem you're experiencing? Compilation errors? Which ones? Or is it just linkage errors?
Danny at 2007-11-11 21:02:39 >
# 3 Re: Making class a template
Thanks Danny,
I have managed to solve the problem. The reason i could not compile my file was because i had declared my overloaded istream and ostream operators as 'friends'. I moved them out of the class and seems OK now.

Thanks again
alexteslin at 2007-11-11 21:03:37 >
# 4 Re: Making class a template
You may find this article useful then:
http://www.dev-archive.com/cplus/10MinuteSolution/30302/
Notice also that VC6 used to have a bug with friend declarations. Well, it had a bug with almost everything but this one was really a nuisance...
Danny at 2007-11-11 21:04:42 >