Thread: Problem with vectors

  1. #1
    sockets mad
    Join Date
    Mar 2002
    Posts
    126

    Problem with vectors

    Hi,

    I've just started using vectors. I've decided to use one to hold connection info for incoming client connections in my program.

    However, VC++ fails to compile at the "v_clients::iterator i;" line in build_fdset with this error:

    Error C2510: 'v_clients' : left of '::' must be a class/struct/union

    I understand this error, but I can't seem to work out where I've gone wrong. I've got a feeling it's going to be something obvious, but I can't find it =)

    Here's the pieces of relevant code cut out and put together:

    Code:
    #include <vector>
    #include <winsock2.h>
    
    using namespace std;
    
    typedef struct
    {
      struct sockaddr_in sin;
      SOCKET sock;
    } cnx_inf;
    
    typedef vector<cnx_inf> cnx_inf_vector;
    
    cnx_inf_vector v_clients;
    
    void build_fdset(fd_set *fds, SOCKET sock_Serv)
    {
    	v_clients::iterator i;
    
    	FD_ZERO(fds);
    	FD_SET(sock_Serv,fds);
    	
    	for (i = v_clients.begin(); i != v_clients.end(); i++)
        {
             FD_SET(*i.sock,fds);
        }
    }
    Any help greatly appreciated.

    Many thanks,

    Daniel

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    its been a while but i think it should be

    cnx_inf_vector::iterator i;

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    cnx_inf_vector::iterator i;

  4. #4
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Thanks guys

    Record time, fixed within 6 minutes

  5. #5
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    I've got another problem related to the same code.

    The line

    Code:
     FD_SET(*i.sock,fds);
    throws an error (which is rather long), basically telling me that *i.sock is not valid. How can I get to the member variables of the struct in each element of the vector?

    Many thanks,

    Daniel

  6. #6
    sockets mad
    Join Date
    Mar 2002
    Posts
    126
    Forget it, I eventually found the answer I was looking for after doing several searches.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't leave a thread hang'n.....

    Readers of this thread might benefit from the answer you found as well.

    gg

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    It should have been: (*i).sock

    The member operator has higher precedence than the dereference operator so you have to use parens to make sure the dereference is done first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with vectors and classes
    By applenerd in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 06:36 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Vector Problem
    By Morgul in forum Game Programming
    Replies: 16
    Last Post: 02-25-2006, 03:27 PM
  4. Problem with Vectors And Pointers
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 01-21-2006, 07:23 PM
  5. Shell Sort with Vectors Problem
    By TheSpoiledMilk in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2005, 03:05 PM