Solved

Whats wrong with this code?

Posted on 2003-10-22
10
229 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now