[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
PLEASE HELP! Bug report when trying to use function objects.
From: |
BOLANOS,FERNANDO (HP-Boise,ex1) |
Subject: |
PLEASE HELP! Bug report when trying to use function objects. |
Date: |
Fri, 29 Jun 2001 17:56:15 -0400 |
/*
This program is for testing object
functions in C++.
HOW IT WORKS:
1. There is a global list of
"student" objects.
2. There is an array of applicant names.
3. The main program tries to
enroll all the students in the
array of aplicants by using
the "findStudent" function.
4. The "findStudent" function uses the
"find_if" generic algorithm
to verify if the applicant
name already exists in the
students list.
5. The
"find_if" generic algorithm uses the function object
"signedUp" as the passed unitary function. This function
object
stores the applicant name when an instance is
created and then
returns a boolean value depending on if
such applicant name is
equal or not to the one passed by
the "find_if" function.
As up today we get the following
error message when trying to
compile this program:
/opt/gnu/lib/g++-include/algo.h: In
function `class list<student>::iterator find_if(class
list<student>::iterator, class list<student>::iterator, class
signedUp)':
/opt/gnu/lib/g++-include/algo.h:80: Internal compiler
error.
/opt/gnu/lib/g++-include/algo.h:80: Please submit a full bug report to
address@hidden'.
*/
#include <stdlib.h>
#include
<iostream.h>
#include <string>
#include
<list>
#include <algorithm>
// 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();
void displaySchool();
// Support Functions Definitions
ostream & operator <<
(ostream & o, student & s)
{
o << s.name() <<
endl;
return(o);
}
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);
}
void
displaySchool()
{
list<student>::iterator start =
school.begin();
list<student>::iterator stop =
school.end();
for (; start != stop;
++start)
{
cout <<
*start;
}
}
// 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);
}
Department Laserjet Printing
Research &
Development
Fernando Bolaños Orozco
Tel: (208) 396-4758
Fax: (208) 938-4122
E-mail: address@hidden
Fernando Bolaños Orozco (E-mai l).vcf
Description: Binary data
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- PLEASE HELP! Bug report when trying to use function objects.,
BOLANOS,FERNANDO (HP-Boise,ex1) <=