bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: PLEASE HELP! Bug report when trying to use function objects.


From: user
Subject: Re: PLEASE HELP! Bug report when trying to use function objects.
Date: 01 Jul 2001 19:49:10 GMT

All work fine with few modifications and the G++ 3.0
//
Try the code below!
//
// 
#include <stdlib.h>
#include <iostream>
#include <string>
#include <list>
#include <algorithm>

using namespace std ;
 
// This is the declaration of the student class.
class student 
{
public:
 // Constructors.
 student() : nameText(NULL) { }
 student(string n) : nameText(n) { }
 
 // Operations.
 string name() { return(nameText); }
 void addCourse(string & course)
  { coursesList.push_back(course); }
 list<string>::iterator firstCourse() 
  { return(coursesList.begin()); }
 list<string>::iterator lastCourse() 
  { return(coursesList.end()); }
 void removeCourse(list<string>::iterator & citr) 
  { coursesList.erase(citr); }
 
private:
 string       nameText;
 list<string> coursesList;
};
 
// This is the declaration of the function object that will test
// the student object for a specific name.
class signedUp
{
public:
 // Constructor.
 signedUp(const char * n) : candidate(n) { }
 
 // Student name variable (note that it is a public variable).
 string candidate;
 
 // Function call operator.
 bool operator () (student & s)
  { return(s.name() == candidate); }
};
 

// Support Functions Prototypes
ostream & operator << (ostream &, student &);
bool findStudent(const char*);
void displaySchool();
 

// Support Functions Definitions
ostream & operator << (ostream & o, student & s)
{
 o << s.name() << endl;
 return(o);
}
 
// This list contains the data of all the students.
list<student> school; 
 
// Main Function.
int main()
{
 char * applicants[4] =
 {
  "Fernando Bolanos",
  "Cesar Ochoa",
  "Ricardo Osuna",
  "Cesar Ochoa"
 };
 
 // Signs students up.
 for (int i = 0; i < 4; ++i)
 {
  if (findStudent(applicants[i]))
  {
   cout << applicants[i] << " is already a student." << endl;
  }
  else
  {
   cout << applicants[i] << " has been signed up." << endl;
  }
 }
 
 // Displays students list.
 cout << endl << "This is the school list: " << endl;
 displaySchool();
 
 return(0);
}


void displaySchool()
{
 list<student>::iterator start = school.begin();
 list<student>::iterator stop  = school.end();

 for (; start != stop; ++start)
 {
  cout << *start;
 }
}

bool findStudent(const char * applicantName)
{
 signedUp suTest(applicantName);

 if (find_if(school.begin(), school.end(), suTest) == school.end())
 {
  student candidate(applicantName);
  school.push_back(candidate);
  return(false);
 }
 return(true);
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]