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
Solved

Whats wrong with this code?

Posted on 2003-10-22
10
232 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

839 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