Thread: class template

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    class template

    Hi
    I have made a template which is suppose to to be a generic container for other data types suchs as int, floats and soo. it works fine with int, floats. But it doesn´t work with my own class type XTime could any one help me with this.


    The function below gives me the error code "illegal structure operation for the if statement.
    template <class Type>
    bool G_container<Type>::contains(Type x) const{
    for(int i=0;i<storlek;i++)
    if(x == array[i])
    return true;

    return false;
    }


    Also second question does any one know how I can specify an otream overload for XTime in my container class. This one also gives me the error code ileegal structure operation. . I tried the following overload but it doesnt work.

    template <class XTime>
    ostream &operator<<(ostream &os, const G_container<XTime> &x){
    for(int i=0;i< x.storlek;i++)
    os << x.array[i].display(true) << ' ';
    os << "\nSize " << x.storlek << " Allokerad " << x.allokerad;
    return os;
    }


    The XTime class is declared as follows:
    class XTime{
    public:
    //Konstruktor
    XTime(): t(0), m(0), s(0) {}
    XTime (int tim, int min, int sek);
    void set(int tim, int min, int sek);
    int read_tim() const {return tim;}
    int read_min() const {return m;}
    int read_sek() const {return s;}
    void display(bool write_sec) const;//display t,m,s

    // some operator/s are needed here,
    // if this class is to work with G_container

    private:
    int t, m, s;
    };

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    >if(x == array[i])
    >return true;

    Is bool operator==(const XTime& xt) const; defined for XTime?
    If not you will need it.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    Thanks

    No it wasn´t it totally slipped my mind.
    Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template and friend class
    By black_spot1984 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2008, 05:50 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Instantiating a template class
    By NullS in forum C++ Programming
    Replies: 11
    Last Post: 02-23-2005, 10:04 AM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM