Link to home
Start Free TrialLog in
Avatar of RiMZ
RiMZ

asked on

Whats wrong with this code?

When I compile this I get these 2 errors.
program2.cpp: undefined reference to `kontrola(MyList<Associate> &)'
program2.cpp: undefined reference to `promote(MyList<Associate> &)'

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

#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <list>
#include <stdio.h>
#include <algorithm>
#include <D:/C++/associate.h>

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

}
Avatar of bobbit31
bobbit31
Flag of United States of America image

kontrola() and promote() are member functions of your class MyList... hence, it should look more like this:

#include <iostream.h>
#include <stdlib.h>
#include <string>
#include <list>
#include <stdio.h>
#include <algorithm>
#include <D:/C++/associate.h>

typedef list<Associate>::iterator iter;

template <class T>
class MyList: public list<T>
{
   // you don't have to pass the list b/c this object is your list so just use 'this' (same for promote)
   void kontrola()
   {
    int i=1;
    for (iter through=this.begin(); through!=this.end(); ++through)
    {
          cout << i << ". number is:" << (*through).getName() << "\t" << endl;
          i++;
    }
   }

   void promote()
   {
     cout << "Enter keyword to promote its position: ";

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

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

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";
   }

    lista.kontrola();
    lista.promote();

}
Avatar of n_fortynine
n_fortynine

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

Avatar of RiMZ

ASKER

bobbit31 That created more errors:

18 'void MyList<Associate>::kontrola()' is private
72 within this context
72 instantiated from here
20 request for member 'begin' in 'this', which is of non-aggregate type 'MyList<Associate>
20 request for member 'end' in 'this', which is of non-aggregate type 'MyList<Associate>
28 'void MyList<Associate>::promote()' is private
73 within this context
73 instantiated from here
31 request for member 'begin' in 'this', which is of non-aggregate type 'MyList<Associate>
31 request for member 'end' in 'this', which is of non-aggregate type 'MyList<Associate>

put

public:

on the line before my comment (// you don't have to pass ...)
Avatar of RiMZ

ASKER

72 instantiated from here
20 request for member 'begin' in 'this', which is of non-aggregate type 'MyList<Associate>
20 request for member 'end' in 'this', which is of non-aggregate type 'MyList<Associate>
73 instantiated from here
31 request for member 'begin' in 'this', which is of non-aggregate type 'MyList<Associate>
31 request for member 'end' in 'this', which is of non-aggregate type 'MyList<Associate>
31 request for member 'begin' in 'this', which is of non-aggregate type 'MyList<Associate>
31 request for member 'begin' in 'this', which is of non-aggregate type 'MyList<Associate>
hrmm... it's been a while since i've done c++

since this is inherits from the list class

this.end() and this.begin() should be ok so long as end() and begin() are public member functions in class list.

maybe try replacing 'this' w/ 'super'
Avatar of RiMZ

ASKER

Nope it said 'super' undeclared (first use of this function)

Is anyone else reading this that can help?
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RiMZ

ASKER

program compiles and will run but then it quits. The window stays open for less than a second. It opens prints the list of objects then quits.
Avatar of RiMZ

ASKER

lol oh it was because I didn't have
      system("PAUSE");
      return 0;

in the main