Link to home
Start Free TrialLog in
Avatar of smithc
smithc

asked on

Templates inside a template

I currently have a fairly basic linklist1 template class set up. I have a list declared as linklist1<Trackdata> trklist. Inside the Trackdata class definition, I also have a  linklist1<Signal>sigList declared.  I have a template function called additem(TYPE d). If I do a trklist.additem(Track), it works fine. But if I do a trklist.siglist.additem(Signal), I get a compile error "Cannot convert parameter from Class Signal to Class Signal" Any idea why this is happening? I have included some source below:
HERE IS MY TEMPLATE DEFINITION:
template<class TYPE>
struct link1{
        TYPE data;
      link1* next;
};

template<class TYPE>
class linklist1{
      private:
            link1<TYPE>* first;
            link1<TYPE>* current;
            link1<TYPE>* previous;
      public:
            linklist1()
            { first = current = previous = NULL;}
            linklist1& operator =(const linklist1& L );
            void additem(TYPE d);            
            void display();
            void Init();
              void GetFirst();
            int Found(TYPE d);
            TYPE GetData(TYPE D);
      };
template<class TYPE>
void linklist1<TYPE>::additem(TYPE d){
link1<TYPE>* newlink = new link1<TYPE>;

      newlink->data = d;
      newlink->next = 0;

        if(!Found(d)){
          if(previous){
                previous->next = newlink;
                newlink->next = current;
          }
          else{
                first = newlink;
                newlink->next = current;
          }
          current = newlink;
       }
}

I DECLARE THIS IN MY DOCUMENT CLASS:
linklist1<Trackdata> trkList;

THE TRACKDATA CLASS IS DECLARED BELOW:
class Trackdata : public CObject
public:
      linklist1<Signal> sig2List;
      linklist1<Message> msg2List;


IN THE DOCUMENT CPP PROGRAM I CALL THE FUNCTION AS SUCH:
Trackdata track1;
Signal sig;
trkList.additem(track1);
track1.sig2List.additem(sig);
      THIS IS WHERE I GET THE COMPILER ERROR!!!!!

Avatar of mnguyen021997
mnguyen021997

what's the definition of your signal class?
Avatar of smithc

ASKER

HERE IS THe SIGNAL CLASS:
class Signal : public CObject
{
private:
      int m_x1pos;      // Signal X1 Position
      int m_x2pos;      // Signal X2 Position
      int m_ypos;              // Signal Y Position
      int m_index;      // Unique Signal Identifier
      CString m_name;      // Signal Name
      int m_timeup;      // Signal TimeUp
      int m_dur;            // Signal Duration
      CString m_mod;      // Signal Modulation
      float m_bw;            // Signal Bandwidth
      CString m_type;      // Signal Type
      float m_ss;            // Signal Strength
      float m_freq;      // Signal Frequency      
      int   m_trkid;      // Signal Track ID
public:
      Signal();      
      ~Signal();      
      int  Getdata(Signal &);
      int Setdata();
      int Retdate();
      void Putdata(float, float, int);
      void operator =(const Signal&);
        int operator <(const Signal);
        int operator ==(const Signal);
      friend istream& operator >>(istream &in, Signal &sd);
      friend ostream& operator <<(ostream &out, Signal &sd);
};
Does your compiler support "nested templates"?  That is, templates declared withing templates?  Some (many?) don't.
Avatar of smithc

ASKER

It's VC++ 5.0 (Pro Edition).  I believe it supports nested templates.........
You are passing arg to additem by value, which means a copy of the Signal value has to be made, but you don't have a copy constructor.

Define a copy constructor for your Signal class.
ASKER CERTIFIED SOLUTION
Avatar of RONSLOW
RONSLOW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smithc

ASKER

Thanks. It worked.