Solved

creating a list template

Posted on 2003-10-22
14
224 Views
Last Modified: 2010-04-02
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.
0
Comment
Question by:RiMZ
  • 6
  • 2
  • 2
  • +2
14 Comments
 

Author Comment

by:RiMZ
ID: 9598533
that last function should be void display not kontrola
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9598694
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)?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9598714
>>#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)?
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 9

Expert Comment

by:tinchos
ID: 9598775
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.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9598785
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;
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 9598926
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();

0
 

Author Comment

by:RiMZ
ID: 9598943
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 :(
0
 

Author Comment

by:RiMZ
ID: 9598959
woops change where it says LinkedList to MyList
0
 
LVL 86

Expert Comment

by:jkr
ID: 9598976
>>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?
0
 

Author Comment

by:RiMZ
ID: 9599031
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.
0
 
LVL 2

Accepted Solution

by:
federal102 earned 250 total points
ID: 9599468
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;
}
0
 

Author Comment

by:RiMZ
ID: 9599793
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

}
0
 
LVL 2

Expert Comment

by:federal102
ID: 9600563
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?
0
 

Author Comment

by:RiMZ
ID: 9600586
Thanks but i posted another question about this and it got answered already
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

803 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