469,923 Members | 1,458 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

std::list pass by reference initialization error

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<HostID>
>&)'
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 HostIDActiveNeighbors;
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!
Jun 27 '08 #1
3 2328
On Apr 30, 8:58*pm, "Ray D." <ray.delvecc...@gmail.comwrote:
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 HostIDActiveNeighbors;
CheckIfNeighborsHaveSentHello(&ActiveNeighbors);
^^^
try :

CheckIfNeighborsHaveSentHello(ActiveNeighbors);

(passing as a reference rather than as a pointer )

(but not tested)

regards
Andy Little
Jun 27 '08 #2
On Apr 30, 9:58 pm, "Ray D." <ray.delvecc...@gmail.comwrote:
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<HostID>
&)'

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 HostIDActiveNeighbors;
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...
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!
Jun 27 '08 #3
utab wrote:
On Apr 30, 9:58 pm, "Ray D." <ray.delvecc...@gmail.comwrote:
>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<HostID>
>>&)'

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 HostIDActiveNeighbors;
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.
>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!


--
Jim Langston
ta*******@rocketmail.com
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Dave | last post: by
8 posts views Thread by JustSomeGuy | last post: by
11 posts views Thread by velthuijsen | last post: by
5 posts views Thread by matthias_k | last post: by
7 posts views Thread by alex221 | last post: by
8 posts views Thread by Spoon | last post: by
17 posts views Thread by Isliguezze | last post: by
11 posts views Thread by Juha Nieminen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.