Solved

Templates inside a template

Posted on 1998-02-19
7
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 104
mixing C++ & C# in Vis Studio 2013 7 239
Create a path if not exists 7 108
c++, dynamic object by json 1 60
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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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++.

726 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