-
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
-
Hi,
I had a problem with tat a few ago myself. Have a read though this thread
http://forums.devx.com/showthread.php?t=150243
VS6might be your problem...
Cheers,
D
-
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 Kalev
-
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
-
You may find this article useful then:
http://www.devx.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 Kalev
Similar Threads
-
Replies: 7
Last Post: 02-02-2006, 09:16 AM
-
Replies: 5
Last Post: 01-15-2006, 08:10 PM
-
By none_none in forum Java
Replies: 17
Last Post: 04-28-2005, 03:00 PM
-
Replies: 5
Last Post: 10-17-2002, 01:58 PM
-
By Shailesh C.Rathod in forum .NET
Replies: 2
Last Post: 03-13-2002, 08:53 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|