Solved

Whats wrong with this code?

Posted on 2003-10-22
10
233 Views
Last Modified: 2010-04-01
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
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
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 9599904
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
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9599913
>> #include <D:/C++/associate.h>
should be #include "D:/C++/associate.h"

0
 

Author Comment

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

Expert Comment

by:bobbit31
ID: 9600045
put

public:

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

Author Comment

by:RiMZ
ID: 9600133
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
 
LVL 18

Expert Comment

by:bobbit31
ID: 9600181
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
 

Author Comment

by:RiMZ
ID: 9600197
Nope it said 'super' undeclared (first use of this function)

Is anyone else reading this that can help?
0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 150 total points
ID: 9600303
ok, try removing the this. and just leaving end() and begin()
0
 

Author Comment

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

Author Comment

by:RiMZ
ID: 9600457
lol oh it was because I didn't have
      system("PAUSE");
      return 0;

in the main
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

733 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