RiMZ
asked on
creating a list template
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.
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[
}
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.
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)?
I haven't seen any implementations of MyList and I'm not sure which is the function that it must contain (is it kontrola)?
>>#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)?
That should be
#include <D:/C++/associate.h>
What other errors are you getting (the code you have *shown* seems OK)?
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.
#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.
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;
}
#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;
}
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();
list<Associate> GetList()
{
return &lista;
}
which allows to call list functions directly from client code:
AssociateList list;
list.GetList()->push_back(
list.GetList()->push_back(
...
list.Display();
ASKER
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 :(
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 :(
ASKER
woops change where it says LinkedList to MyList
>>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?
Why should you?
list<Associate> lista;
should just work fine. *WHAT* errors are you getting?
ASKER
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 guess so I can make different lists with different member functions and not have a lot of member functions just for list.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
}
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,
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<<"\
}
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?
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?
ASKER
Thanks but i posted another question about this and it got answered already
ASKER