Making class a template


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Making class a template

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    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

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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

  4. #4
    Join Date
    Feb 2006
    Posts
    3
    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

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    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

  1. Replies: 7
    Last Post: 02-02-2006, 09:16 AM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 04:00 PM
  4. Replies: 5
    Last Post: 10-17-2002, 02:58 PM
  5. Assembly class
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center