473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<GENCHECKNA MES;
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 1738
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<GENCHECKNA MES;
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<GENCHECKNA MES;
* *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==(cons t GENCHECK& rhs) const
{
return firstName == rhs.firstName;
}

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

bool operator==(cons t 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<GENCHECKNA MES;
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==(cons t GENCHECK& rhs) const
{
return firstName == rhs.firstName;
}

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

bool operator==(cons t 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(st ringValue) do something here? TIA
Jun 27 '08 #5
"Mike Copeland" <mr*****@cox.ne twrote 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<GENCHECKNA MES;
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()(GENC HECK&) 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<GENCHECKNA MES;
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 objektorientier ter Datenverarbeitu ng
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<GENCHECKNA MES;
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
4082
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 already created :/ how to do it? thx.
25
3698
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 when iteration. here is the wrong way I did: lst = for i in lst: print i if i == 2:
0
5046
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 change. After a stop and start of mysql, things progessed normally. Checking back a few days later I noticed it was once again sitting up there at 95% (or thereabouts) and doing nothing of value from what i could tell. Have restarted MySQL during...
7
2291
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 with a recursive
4
3426
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 function used to calculate a specific value. thx
1
4853
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 discover the root cause: We've got an ASP.NET application, running on framework 1.1 on Windows 2003 (IIS 6). Under default settings, during testing by two users, we're seeing the CPU usage on w3wp.exe rocket up and stay up (and thus cause web...
15
3099
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 hashtable or a generic list. Is using enumerators to iterate through the data structure a good idea? I'd appreciate any suggesstions or advice,
1
1592
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
2901
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 looks like this: typedef struct { vector origin; /* vector is an array of 3 doubles */
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9765
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7327
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.