Solved

creating a list template

Posted on 2003-10-22
14
225 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
[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
  • 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
Technology Partners: 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!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

696 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