Re: STL list Usage
On 12 mai, 06:41, mrc2...@cox.net (Mike Copeland) wrote:
I am trying to learn/use the STL <list> to implement a small
application. I didn't get very far before I got a compile error that
befuddles me. Here's the code:
struct GENCHECK // Gender Check data
{
char genCode;
string firstName;
} gWork;
typedef list<GENCHECK> NAMES;
NAMES genData;
list<GENCHECK>::iterator gIter;
class nameEqual : public unary_function<NAMES, bool>
{ // predicate class to perform structure element comparison
string s;
public:
explicit nameEqual (const string &ss) : s(ss) {}
bool operator() (const NAMES &e) const { return e.firstName == s=
; }
};
The error (VS 6.0) is:
That old dog?
Sorry, it's all I have... 8<{{
Both recent VC++ and g++ are very good compilers, and both are
available for free.
error C2039: 'firstName' : is not a member of
'list<struct GENCHECK,class std::allocator<struct GENCHECK> >'
and I don't understand why it fails to compile. This code was cobbled=
from various sources that by themselves worked, but this doesn't...
Well this time the compiler is correct, firstName isn't a member of
std::list.
Okay, so how can I change to code to reference the structure data?
I'm confused why the compiler believes I'm referring to the std::list
type here...
I'm confused why you'd be confused. The type of e is NAMES
const, and NAMES is a typedef for an instantiation of std::list.
How could using it be anything but an std::list.
I'm open to other ways to achieve my goal: which is to populate a
list (or whatever), search it for a match against the string element o=
f
each object, and adding to the list if I don't find a match. The code=
above (so far) is only my attempt to declare the data structures and
define a comparison function. Please advise. TIA
You'll have to use std::find to search your list to see if the name is
present.
I don't see how I can use str::find to make a test on an element of a
structure. As I understand how std:find works, it operates on a scalar,
not a structure. Please explain how I do this. TIA
Find works on anything for which operator== (or operator!=) is
defined, so you can definitely make it work on any user defined
type. Or he may simply have meant std::find_if: I know that I
tend to use std::find as a generic name for both std::find and
std::find_if---which one I actually use will then depend on the
context.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34