473,545 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

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

helpers.cpp:99: error: in passing argument 1 of `void
CheckIfNeighbor sHaveSentHello( std::list<HostI D, std::allocator< HostID>
>&)'
The function is shown below:

void CheckIfNeighbor sHaveSentHello( std::list<struc t HostID>
&Neighbors)
{
std::list<struc t HostID>::iterat or it;
std::list<struc t HostID>::iterat or LastIt;
struct timeb TimeBuffer;
ftime( &TimeBuffer );

it=Neighbors.be gin();
while (it!=Neighbors. end())
{
int Del=0;
if (TimeBuffer.tim e - 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<struc t HostIDActiveNei ghbors;
CheckIfNeighbor sHaveSentHello( &ActiveNeighbor s);
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 2512
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:

maintainNeighbo rs.cpp:104: error: invalid initialization of non-const
reference of type 'std::list<Host ID, std::allocator< HostID&' from a
temporary of type 'std::list<Host ID, std::allocator< HostID*'
<...>
std::list<struc t HostIDActiveNei ghbors;
CheckIfNeighbor sHaveSentHello( &ActiveNeighbor s);
^^^
try :

CheckIfNeighbor sHaveSentHello( 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:

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

helpers.cpp:99: error: in passing argument 1 of `void
CheckIfNeighbor sHaveSentHello( std::list<HostI D, std::allocator< HostID>
&)'

The function is shown below:

void CheckIfNeighbor sHaveSentHello( std::list<struc t HostID>
&Neighbors)
{
std::list<struc t HostID>::iterat or it;
std::list<struc t HostID>::iterat or LastIt;
struct timeb TimeBuffer;
ftime( &TimeBuffer );

it=Neighbors.be gin();
while (it!=Neighbors. end())
{
int Del=0;
if (TimeBuffer.tim e - 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<struc t HostIDActiveNei ghbors;
CheckIfNeighbor sHaveSentHello( &ActiveNeighbor s);
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:

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

helpers.cpp:99 : error: in passing argument 1 of `void
CheckIfNeighbo rsHaveSentHello (std::list<Host ID,
std::allocator <HostID>
>>&)'

The function is shown below:

void CheckIfNeighbor sHaveSentHello( std::list<struc t HostID>
&Neighbors)
{
std::list<struc t HostID>::iterat or it;
std::list<struc t HostID>::iterat or LastIt;
struct timeb TimeBuffer;
ftime( &TimeBuffer );

it=Neighbors.be gin();
while (it!=Neighbors. end())
{
int Del=0;
if (TimeBuffer.tim e - 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<stru ct HostIDActiveNei ghbors;
CheckIfNeighbo rsHaveSentHello (&ActiveNeighbo rs);

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:
CheckIfNeighbor sHaveSentHello( 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*******@rocke tmail.com
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5591
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 storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put...
8
2828
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 list class must Ioverride in order for this to work?
11
5771
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 substraction operator. Peeking in the algoritm header file it's clear why seeing that sort there calls _sort(_First, _Last, _Last-_First) which might be a tad...
5
7350
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 std::list::sort with such a predicate but that doesn't work:
6
6640
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 give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const...
7
2957
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{ Contents cn; list < BTree_node < Contents children; ........
8
3408
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
4151
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 std::list<T>(); } List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); } List(int n, const T& value) { lst = new std::list<T>(n, value); }
11
4145
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' still points to list1::end(). 2) 'iter' now points to list2::end().
0
7484
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7415
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
5997
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1030
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.