Solved

Templates inside a template

Posted on 1998-02-19
7
217 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

808 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