CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2005
    Posts
    59

    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;	
    };

  2. #2
    Join Date
    Feb 2003
    Posts
    377

    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.

  3. #3
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    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

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: STD List matching it iteration

    Please Ignore this post. Apologies.
    Quote 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.

  5. #5
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: STD List matching it iteration

    Quote Originally Posted by exterminator
    Correct me if I am wrong.
    As you wish...
    Quote 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

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: STD List matching it iteration

    Quote 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

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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
  •  





Click Here to Expand Forum to Full Width

Featured