473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

STL list Usage

I am trying to learn/use the STL <listto 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<GENCHECKNAMES;
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:

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...
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 of
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
Jun 27 '08 #1
8 1713
Mike Copeland wrote:
I am trying to learn/use the STL <listto 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
Use of all caps for types isn't a good idea, it makes them look like macros.
{
char genCode;
string firstName;
} gWork;
typedef list<GENCHECKNAMES;
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?
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.
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 of
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.

--
Ian Collins.
Jun 27 '08 #2
On May 11, 7:09*pm, mrc2...@cox.net (Mike Copeland) wrote:
* *I am trying to learn/use the STL <listto 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<GENCHECKNAMES;
* *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; }

};
Instead of creating a "nameEqual" predicate, I would make it possible
to compare "GENCHECK" objects directly:

struct GENCHECK // Gender Check data
{
char genCode;
string firstName;

bool operator==(const GENCHECK& rhs) const
{
return firstName == rhs.firstName;
}

bool operator<(const GENCHECK& rhs) const
{
return firstName < rhs.firstName;
}

bool operator==(const std::string& rhs) const
{
return firstName == rhs;
}

bool operator<(const std::string& rhs) const
{
return firstName < rhs;
}
} gWork;

With these operators defined, it is now possible to sort the list of
GENCHECK objects (by calling std::list.sort()) or to find a particular
object that matches a string (by calling std::find() on the list).

Greg

Jun 27 '08 #3
I am trying to learn/use the STL <listto 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

Use of all caps for types isn't a good idea, it makes them look like macros.
Fair enough - it just is the style I've been using, but I understand
your comment.
>
{
char genCode;
string firstName;
} gWork;
typedef list<GENCHECKNAMES;
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<{{
>
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 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 of
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
Jun 27 '08 #4
Instead of creating a "nameEqual" predicate, I would make it possible
to compare "GENCHECK" objects directly:

struct GENCHECK // Gender Check data
{
char genCode;
string firstName;

bool operator==(const GENCHECK& rhs) const
{
return firstName == rhs.firstName;
}

bool operator<(const GENCHECK& rhs) const
{
return firstName < rhs.firstName;
}

bool operator==(const std::string& rhs) const
{
return firstName == rhs;
}

bool operator<(const std::string& rhs) const
{

bool operator<(const std::string& rhs) const
{
return firstName < rhs;
}
} gWork;
With these operators defined, it is now possible to sort the list of
GENCHECK objects (by calling std::list.sort()) or to find a particular
object that matches a string (by calling std::find() on the list).
Okay, but how do I _use_ these objects in my working code? I added
your code to mine, removed the class/bool that was failing to compile,
and the code now compiles. However, I don't know how to actually engage
the structure functions in my logic. (This is a new concept for me...)
Does genData.find(stringValue) do something here? TIA
Jun 27 '08 #5
"Mike Copeland" <mr*****@cox.netwrote in message
news:MP************************@news.cox.net...
I am trying to learn/use the STL <listto 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

Use of all caps for types isn't a good idea, it makes them look like
macros.

Fair enough - it just is the style I've been using, but I understand
your comment.
>>
{
char genCode;
string firstName;
} gWork;
typedef list<GENCHECKNAMES;
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<{{
[...]

You can get something much better for free:

http://www.microsoft.com/express/product/default.aspx

Jun 27 '08 #6
Mike Copeland wrote:
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; }
};
NAMES is std::list. And std::list doesn't have firstName.
Change the NAMES to GENCHECK.:
class nameEqual : public unary_function<GENCHECK, bool>
[...]
bool operator() (const GENCHECK &e) const { return e.firstName == s; }
[...]

Why, well find_if will iterate through the given range of your container
holding GENCHECK elements and will do a function call to the provided
predicate object's operator()(GENCHECK&) like this "_predicate()(*iter)".
--
Sashi Asokarajan
Jun 27 '08 #7
On 12 mai, 06:41, mrc2...@cox.net (Mike Copeland) wrote:
I am trying to learn/use the STL <listto 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<GENCHECKNAMES;
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 of
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:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
Mike Copeland wrote:
I am trying to learn/use the STL <listto 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<GENCHECKNAMES;
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:

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...

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 of
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.
but you want to compare GENCHECKs (structs) not NAMESs (lists).

--
Nick Keighley
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it...
25
by: skull | last post by:
Hi everybody, it is my first post in this newsgroup. I am a newbie for python though I have several years development experience in c++. recently, I was stumped when I tried to del item of a list...
0
by: Dave [Hawk-Systems] | last post by:
Occasionally in checking one of the servers, I noticed that mysql shows 85% + of cpu usage essentially leaving the server at 0% idle. After monitoring it for a few hours, the status did not...
7
by: Evangelista Sami | last post by:
Hi all i have implemented a list type as an array of pointer like this typedef struct { int nb_elements; void **elements; } list; to avoid having a pointer for each element as it is done...
4
by: Hermann Maier | last post by:
hi, i need to find out the memory usage of a specific function that i use in my program. this function does some recursive calculations and i want my program to display the amount of memory the...
1
by: Damien | last post by:
Hi guys, I'm looking for ideas for troubleshooting the following. We've tried some random things to try to treat the symptoms, but none seem robust enough to use when we go live, and we'd rather...
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
1
by: fishscience | last post by:
I have a problem about memory usage. The code is as below: #include <list> int main(int argc, char* argv) { { std::list<void*ptr_list; { for (int i=0; i<9368; i++) {
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.