473,832 Members | 2,181 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 2534
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
5639
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 if we've got pointers to them somewhere! Am I correct in my assertion?
8
2851
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
5800
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 challenging seeing that the different values stored in a list do not need to be stored...
5
7364
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
6666
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 T*>::iterator?
7
2984
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
3430
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
4199
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
4179
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
9642
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10540
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5623
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5789
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4421
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
2
3970
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.