• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

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

}
0
RiMZ
Asked:
RiMZ
  • 5
  • 4
1 Solution
 
bobbit31Commented:
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();

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

0
 
RiMZAuthor Commented:
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>

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
bobbit31Commented:
put

public:

on the line before my comment (// you don't have to pass ...)
0
 
RiMZAuthor Commented:
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>
0
 
bobbit31Commented:
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'
0
 
RiMZAuthor Commented:
Nope it said 'super' undeclared (first use of this function)

Is anyone else reading this that can help?
0
 
bobbit31Commented:
ok, try removing the this. and just leaving end() and begin()
0
 
RiMZAuthor Commented:
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.
0
 
RiMZAuthor Commented:
lol oh it was because I didn't have
      system("PAUSE");
      return 0;

in the main
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now