std::list pass by reference initialization error

R

Ray D.

Hey all,

I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:

maintainNeighbors.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<HostID, std::allocator<HostID> >&' from a
temporary of type 'std::list<HostID, std::allocator<HostID> >*'

helpers.cpp:99: error: in passing argument 1 of `void
CheckIfNeighborsHaveSentHello(std::list said:

The function is shown below:

void CheckIfNeighborsHaveSentHello(std::list<struct HostID>
&Neighbors)
{
std::list<struct HostID>::iterator it;
std::list<struct HostID>::iterator LastIt;
struct timeb TimeBuffer;
ftime( &TimeBuffer );

it=Neighbors.begin();
while (it!=Neighbors.end())
{
int Del=0;
if (TimeBuffer.time - it->LastHelloRec > 40)
Del=1;
LastIt = it;
++it;
if (Del==1)
Neighbors.erase(LastIt);
}
}

And the objects contained in the list are shown below, along with how
it is defined and the function call itself:

struct HostID {
char IP[16];
int Port;
int LastHelloRec;
int LastHelloSent;
};

std::list<struct HostID> ActiveNeighbors;
CheckIfNeighborsHaveSentHello(&ActiveNeighbors);


My guess is that it has something to do with the iterator, but I've
been stuck on this for a while now and I figure a more experienced
person could guide me in the right direction. Thanks in advance for
any help!
 
K

kwikius

Hey all,

I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:

maintainNeighbors.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<HostID, std::allocator<HostID> >&' from a
temporary of type 'std::list<HostID, std::allocator<HostID> >*'

std::list<struct HostID> ActiveNeighbors;
CheckIfNeighborsHaveSentHello(&ActiveNeighbors);
^^^
try :

CheckIfNeighborsHaveSentHello(ActiveNeighbors);

(passing as a reference rather than as a pointer )

(but not tested)

regards
Andy Little
 
U

utab

Hey all,

I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:

maintainNeighbors.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<HostID, std::allocator<HostID> >&' from a
temporary of type 'std::list<HostID, std::allocator<HostID> >*'

helpers.cpp:99: error: in passing argument 1 of `void
CheckIfNeighborsHaveSentHello(std::list said:

The function is shown below:

void CheckIfNeighborsHaveSentHello(std::list<struct HostID>
&Neighbors)
{
std::list<struct HostID>::iterator it;
std::list<struct HostID>::iterator LastIt;
struct timeb TimeBuffer;
ftime( &TimeBuffer );

it=Neighbors.begin();
while (it!=Neighbors.end())
{
int Del=0;
if (TimeBuffer.time - it->LastHelloRec > 40)
Del=1;
LastIt = it;
++it;
if (Del==1)
Neighbors.erase(LastIt);
}

}

And the objects contained in the list are shown below, along with how
it is defined and the function call itself:

struct HostID {
char IP[16];
int Port;
int LastHelloRec;
int LastHelloSent;

};

std::list<struct HostID> ActiveNeighbors;
CheckIfNeighborsHaveSentHello(&ActiveNeighbors);

Here is the problem, expecting a reference, not a pointer ...
Reference
in the parameter list becomes an alias for ActiveNeighbors, but you
are
passing the address of the list, which is the cause I guess...
 
J

Jim Langston

utab said:
Hey all,

I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:

maintainNeighbors.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<HostID, std::allocator<HostID> >&' from
a temporary of type 'std::list<HostID, std::allocator<HostID> >*'

helpers.cpp:99: error: in passing argument 1 of `void
CheckIfNeighborsHaveSentHello(std::list<HostID,
std::allocator said:

The function is shown below:

void CheckIfNeighborsHaveSentHello(std::list<struct HostID>
&Neighbors)
{
std::list<struct HostID>::iterator it;
std::list<struct HostID>::iterator LastIt;
struct timeb TimeBuffer;
ftime( &TimeBuffer );

it=Neighbors.begin();
while (it!=Neighbors.end())
{
int Del=0;
if (TimeBuffer.time - it->LastHelloRec > 40)
Del=1;
LastIt = it;
++it;
if (Del==1)
Neighbors.erase(LastIt);
}

}

And the objects contained in the list are shown below, along with how
it is defined and the function call itself:

struct HostID {
char IP[16];
int Port;
int LastHelloRec;
int LastHelloSent;

};

std::list<struct HostID> ActiveNeighbors;
CheckIfNeighborsHaveSentHello(&ActiveNeighbors);

Here is the problem, expecting a reference, not a pointer ...
Reference
in the parameter list becomes an alias for ActiveNeighbors, but you
are
passing the address of the list, which is the cause I guess...

In other words, try changing it to:
CheckIfNeighborsHaveSentHello(ActiveNeighbors);

References work on the instanct name, not the address of the instance.


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top