Solved

Templates inside a template

Posted on 1998-02-19
7
214 Views
Last Modified: 2010-04-10
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!!!!!

0
Comment
Question by:smithc
7 Comments
 
LVL 2

Expert Comment

by:mnguyen021997
ID: 1182541
what's the definition of your signal class?
0
 

Author Comment

by:smithc
ID: 1182542
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);
};
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182543
Does your compiler support "nested templates"?  That is, templates declared withing templates?  Some (many?) don't.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:smithc
ID: 1182544
It's VC++ 5.0 (Pro Edition).  I believe it supports nested templates.........
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1182545
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.
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 110 total points
ID: 1182546
Oops - that should have been an answer... define a copy constructor for Signal
0
 

Author Comment

by:smithc
ID: 1182547
Thanks. It worked.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now