|
-
June 30th, 2005, 07:55 PM
#1
STD List matching it iteration
I'm trying to figure out the best way to match an object to a an object in a list:
Code:
void clientInfo::addToList(clientInfo client){
theList.push_back(client);
}
void clientInfo::delFromList(clientInfo client){
// Create constant iterator for list.
list<int>::const_iterator iter;
// Iterate through list and output each element.
for (iter = theList.begin(); iter != theList.end(); iter++){
if ((*iter) == client)){
MessageBox(NULL, "Match", "List", MB_OK);
}
}
}
Gives me
c:\Documents and Settings\Cody Harris\My Documents\Visual Studio Projects\UpdateServer\clientinfo.cpp(19): error C2059: syntax error : ')'
c:\Documents and Settings\Cody Harris\My Documents\Visual Studio Projects\UpdateServer\clientinfo.cpp(7): error C2664: 'std::list<_Ty>:  ush_back' : cannot convert parameter 1 from 'clientInfo *const ' to 'const clientInfo &'
with
[
_Ty=clientInfo
]
c:\Documents and Settings\Cody Harris\My Documents\Visual Studio Projects\UpdateServer\clientinfo.cpp(18): error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'std::list<_Ty>::const_iterator' (or there is no acceptable conversion)
with
[
_Ty=int
]
c:\Documents and Settings\Cody Harris\My Documents\Visual Studio Projects\UpdateServer\clientinfo.cpp(19): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::allocator<_Ty>::value_type' (or there is no acceptable conversion)
with
[
_Ty=int
]
c:\Documents and Settings\Cody Harris\My Documents\Visual Studio Projects\UpdateServer\clientinfo.cpp(18): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::list<_Ty>::iterator' (or there is no acceptable conversion)
with
[
_Ty=clientInfo
]
]
The clientinfo class is as follows:
Code:
class clientInfo{
public:
//Constuctors/Destructors
clientInfo(SOCKET sock, LPSOCKADDR lpSockAddrIn, int nAddrLen);
//Public Functions:
int matchSock(SOCKET sock);
int getState();
void setState();
//Static functions
static clientInfo findClient(SOCKET sock);
static void addToList(clientInfo client);
static void delFromList(clientInfo client);
private:
SOCKET clientSock;
LPSOCKADDR lpSockAddr;
static list<clientInfo> theList;
};
-
June 30th, 2005, 08:12 PM
#2
Re: STD List matching it iteration
This
Code:
list<int>::const_iterator iter
should be this
Code:
list<clientInfo>::const_iterator iter
You also need to define an operator== for your clientInfo class so you can compare separate instances of the class.
It might be better if you used a set (or unordered_set) instead of a list, and define a less than function for your clientInfo class. That way you could simply use the find() method to see if the client existed in the set. You can use find() for lists, but the set maintains the sorting of the elements which makes lookups faster.
-
June 30th, 2005, 08:20 PM
#3
Re: STD List matching it iteration
c:\Documents and Settings\Cody Harris\My Documents\Visual Studio Projects\UpdateServer\clientinfo.cpp(19): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::allocator<_Ty>::value_type' (or there is no acceptable conversion)
with
[
_Ty=int
]
This error is caused because you didn't define an operator== function for the clientInfo class. You need to provide this operator for you to be able to compare clientInfo objects for equality:
Code:
class clientInfo{
public:
//Constuctors/Destructors
clientInfo(SOCKET sock, LPSOCKADDR lpSockAddrIn, int nAddrLen);
//Public Functions:
int matchSock(SOCKET sock);
int getState();
void setState();
bool operator==(const clientInfo& other) const;
//Static functions
static clientInfo findClient(SOCKET sock);
static void addToList(clientInfo client);
static void delFromList(clientInfo client);
private:
SOCKET clientSock;
LPSOCKADDR lpSockAddr;
static list<clientInfo> theList;
};
Old Unix programmers never die, they just mv to /dev/null
-
July 1st, 2005, 12:47 AM
#4
Re: STD List matching it iteration
Please Ignore this post. Apologies.
 Originally Posted by HighCommander
This error is caused because you didn't define an operator== function for the clientInfo class. You need to provide this operator for you to be able to compare clientInfo objects for equality:
The comparison would lead to bad results but it should not show errors if the OPer has not defined the '==' operator explicitly by himself. That is there with the classes that you make by default ..doesnt it?
And what you have provided is similar to what is provided by default. I know since he has got the reference members he needs to define it. But the error message says - "there is no acceptable conversion". This is because of the improper usage of the "==" operator. I guess jlou's correction would rectify that problem. But, I would suggest the implementation of the "==" operator explicitly in the ClientInfo class.
Correct me if I am wrong.
Cheers,
Exterminator.
Last edited by exterminator; July 2nd, 2005 at 12:42 AM.
-
July 1st, 2005, 10:59 AM
#5
Re: STD List matching it iteration
 Originally Posted by exterminator
Correct me if I am wrong.
As you wish...
 Originally Posted by exterminator
The comparison would lead to bad results but it should not show errors if the OPer has not defined the '==' operator explicitly by himself. That is there with the classes that you make by default ..doesnt it?
No == operator is provided by default. If you want one, you have to make it yourself.
Insert entertaining phrase here
-
July 1st, 2005, 02:16 PM
#6
Re: STD List matching it iteration
 Originally Posted by exterminator
The comparison would lead to bad results but it should not show errors if the OPer has not defined the '==' operator explicitly by himself. That is there with the classes that you make by default ..doesnt it?
And what you have provided is similar to what is provided by default. I know since he has got the reference members he needs to define it. But the error message says - "there is no acceptable conversion". This is because of the improper usage of the "==" operator. I guess jlou's correction would rectify that problem. But, I would suggest the implementation of the "==" operator explicitly in the ClientInfo class.
Correct me if I am wrong.
Cheers,
Exterminator.
AFAIK, the only 5 functions that are defined automatically for a class are:
- default constructor (if you don't provide any constructors)
- copy constructor
- assignment operator
- address operator
- destructor
Gurus, please correct me if I missed any.
Old Unix programmers never die, they just mv to /dev/null
-
July 2nd, 2005, 12:41 AM
#7
Re: STD List matching it iteration
Aahh...I am sorry about my earlier post. Thanks wien and HighCommander4 for correcting me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|