creating a list template

RiMZ
RiMZ used Ask the Experts™
on
I created a list holding "Associate" objects and a function which iterates though the list and displays the "name" attribute of each object.

But I need help creating a list class (MyList) which inherits the stl list and has the function i created as a member function.

The code I have is this:
//============================================================
#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <list>
#include <stdio.h>
#include <algorithm>
#include <D:\C++\associate.h>

    Associate TheArray[200];       //Make an array to hold Associate Objects.
    typedef list<Associate>::iterator iter;
    void display(list<Associate>&);

int main()
{
      //THIS NEXT BLOCK - gets info from an input file and puts the Associate objects in an array
     //-------------------------------------------------------------------------------------------------------
      int i=0;
      while(!in_put.eof()) {
                  TheArray[i].getInfo();          // Put Associate objects in TheArray[]
                  i++;
      }
      in_put.close();
    //----------------------------------------------------------------------------------------------------------
   
  list<Associate> lista;       // Make a list called lista

   for ( int i = 0; i < 25; i++)
   {
     lista.push_back(The_Array[i]);
   }

    display(lista); // Iterate though list and display the "name" attribute of each object

      system("PAUSE");
      return 0;
}


void kontrola(list<Associate>& lista)
{
    int i=1;
    for (iter through=lista.begin(); through!=lista.end(); ++through)
    {
          cout << i << ". number is:" << (*through).getName() << "\t" << endl;
          i++;
    }
}

//============================================================

I've been working on this a little for the last few days and a lot for the last few hours but I don't know if what i'm doing is even close to right. I don't know if my list class structure is correct, I've been looking at exampls and doing a lot of trial and error but keep getting compiling errors when I try to integrate the working code I have with the list class I tried to create.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
that last function should be void display not kontrola

Commented:
Sorry Rimz, but I'm not sure if i got your idea right

I haven't seen any implementations of MyList and I'm not sure which is the function that it must contain (is it kontrola)?
jkr
Top Expert 2012

Commented:
>>#include <D:\C++\associate.h>

That should be

#include <D:/C++/associate.h>

What other errors are you getting (the code you have *shown* seems OK)?
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Commented:
Another possibility to what jkr says is

#include "D:\C++\associate.h"            

I'm not sure if it a standard (even though I believe so), but in MsVc6 it works

but I believe that the problem is more than that

>> But I need help creating a list class (MyList) which inherits the stl list and has the function i created as a member function.

Commented:
What about writing the list<Associate> wrapper class:


#include <D:\C++\associate.h>

typedef list<Associate>::iterator iter;

class AssociateList
{
public:
    AssociateList(){}
    virtual ~AssociateList(){}

    // add wrapper to every list function required by client:
    void push_back(Associate a)
    {
          lista.push_back(a);
    }
   
    // another wrapping functions which call to appropriate lista functions
    // ...

    // additional functions:
    void Display()
    {
       int i=1;
       for (iter through=lista.begin(); through!=lista.end(); ++through)
       {
             cout << i << ". number is:" << (*through).getName() << "\t" << endl;
             i++;
       }
    }


protected:
    list<Associate> lista;
}

Commented:
This class may expose also it's internal list<Associate> object:

list<Associate> GetList()
{
    return &lista;
}

which allows to call list functions directly from client code:

AssociateList list;

list.GetList()->push_back(a1);
list.GetList()->push_back(a2);
...
list.Display();

Author

Commented:
Thanks but I need to use templates. And isn't there some way to inherit the list so I don't have to remake the push_back function?

I was thinking something like this.

typedef list<Associate>::iterator iter;
// Forward declaration of class LinkedList
template <class t> class LinkedList;

template <class t>
class LIST
{
   public:
    friend class MyList<t>;

};


template <class t>
class MyList{
   public:

   public:
    MyList() {}
    ~MyList() {}

void display(list<t>& *lista);

};

Then in main calling
MyList<Associate> lista;      // BUT THIS DOES NOT WORK (WHY?)


template <class t>
void display(list<t>& *lista){
    int i=1;
    for (iter through=lista.begin(); through!=lista.end(); ++through)
    {
          cout << i << ". number is:" << (*through).getName() << "\t" << endl;
          i++;
    }
}


//BUT THIS DOES NOT WORK :(

Author

Commented:
woops change where it says LinkedList to MyList
jkr
Top Expert 2012

Commented:
>>And isn't there some way to inherit the list

Why should you?

list<Associate> lista;

should just work fine. *WHAT* errors are you getting?

Author

Commented:
Because its a requirement that I make an ADT that is a templated extension the STL list.
I guess so I can make different lists with different member functions and not have a lot of member functions just for list.
I think this is what you need...

template <class T>
class MyList: public list<T>
{
  //your MyList specific code
};

int main()
{
  MyList<int> p;
  p.push_back(1);
  p.push_back(2);
  MyList<int>::iterator i = p.begin();
  while(i != p.end())
  {
    cout << *i << endl;
    ++i;
   }
   return 0;
}

Author

Commented:
Its compiling but I get these errors
program2.cpp: undefined reference to `kontrola(MyList<Associate> &)'
program2.cpp: undefined reference to `promote(MyList<Associate> &)'

Can someone spot what I did wrong?
//===============================================================



typedef list<Associate>::iterator iter;

template <class T>
class MyList: public list<T>
{
   void kontrola(MyList<T>& lista)
   {
    int i=1;
    for (iter through=lista.begin(); through!=lista.end(); ++through)
    {
          cout << i << ". number is:" << (*through).getName() << "\t" << endl;
          i++;
    }
   }

   void promote(MyList<T>& lista)
   {
     cout << "Enter keyword to promote its position: ";

         for (iter through=lista.begin(); through!=lista.end(); ++through)
         {
          if( (*through).getName() == "Albania ")
          { cout<<"ALBANIA WAS FOUND!!!!!!!!!!!!!!!!!!!! \n\n";}
         }
   }
};

    Associate UN_Array[200];       //Make an array to hold Associate Objects.

    void kontrola(MyList<Associate>&);
    void promote(MyList<Associate>&);


int main()
{

      ifstream in_un; //variable for input file name
      in_un.open("UN.txt"); //name of file to open as input file
      
      int i=0, PhoneIndent = 18; // start the phone member 18 chars over
      while(!in_un.eof())// while not at end of file
      {
            UN_Array[i].getInfo(in_un, PhoneIndent); //get information from inputfile
                                cout<<UN_Array[i].name<<"";
            i++;
      }
                in_un.close();             //close input file stream

   //CREATE LIST
   MyList<Associate> lista;
   //PUT INFO IN LIST
   for ( int i = 0; i < 25; i++)
   {
      lista.push_back(UN_Array[i]);
      cout<<UN_Array[i].name<<"\n";
   }

    kontrola(lista); // IF I COMMENT OUT THIS CALL THE ERROR GOES AWAY
    promote(lista); // IF I COMMENT OUT THIS CALL THE ERROR GOES AWAY

}
The code..

kontrola(lista);
promote(lista);

is calling the global functions that you have declared, but not defined as follows..

void kontrola(MyList<Associate>&);
void promote(MyList<Associate>&);

i.e you have stated that the functions exist, but have not provided any code to define them. Did you mean to call the member functions of your list?

Author

Commented:
Thanks but i posted another question about this and it got answered already

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial