473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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 2498
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
11
by: velthuijsen | last post by:
I tried taking a list and pass it through std::sort like the following: sort(Unsorted.begin(), Unsorted.end()); I got an error back stating that the list iterator doesn't have a binary...
5
by: matthias_k | last post by:
Hi, I need to sort elements of a std::list using a function predicate, something like: bool predicate( const& M m1, const& M m2 ) { return m1.somedata < m2.somedata; } I tried to call...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
11
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter'...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.