Simon Fransson
Simon Fransson

Reputation: 3

Passing iterator value rather than reference to function

I'm trying to pass my struct in an iterator into a function that accesses the struct using the "."-operator. I'm trying to wrap my head around the difference of passing values, pointers and addresses and I just can't figure this one out.

Below is the error from the compiler and the code that generates the error.

Compiler error:

server-iterative.cpp:222:109: error: invalid initialization of reference of type ‘const ConnectionData&’ from expression of type ‘ConnectionData*’ server-iterative.cpp:134:13: error: in passing argument 1 of ‘bool is_invalid_connection(const ConnectionData&)’

Code:

struct ConnectionData
{
    EConnState state;
    int sock;
    size_t bufferOffset, bufferSize; 
    char buffer[kTransferBufferSize+1];
};

for(std::vector<ConnectionData>::iterator it = connections.begin(); it != connections.end(); ++it){
    if(FD_ISSET(it->sock, &rset)){
        if(process_client_recv(*it) == false){
            close(it->sock);
            bool test = is_invalid_connection(&(*it));
        }
    }
}

static bool is_invalid_connection( const ConnectionData& cd )
{
    return cd.sock == -1;
}

Upvotes: 0

Views: 1127

Answers (1)

john
john

Reputation: 87959

Like this bool test = is_invalid_connection(*it);

it is an iterator to ConnectionData (in a vector) so *it is the data itself.

Upvotes: 1

Related Questions