STL list insertion compiler error

O

OMouse

Hi, I just switched to using STL for my linked lists and obviously I
need a way to insert. I have all the necessary includes (list &
algorithm) and the other functions that I've used (erase & find) work.
But when I try to insert the iterator that was found, gcc 3.3.4 tells
me that I'm missing a few inputs. Anyway, here's the snippet of code.

main.cpp
-----------------
#include "usernode.h"
#include <list>
#include <algorithm>
....
userNode tempNode = userNode(user, pword, NULL, NULL, NULL, false,
NULL);
list<userNode>::iterator listNode;
listNode = find(usersOnline.begin(), usersOnline.end(), tempNode);
if (listNode != usersOnline.end())
{
usersOnline.insert(usersOnline.begin(), listNode);
}
....
-----------------

Error message(s)
-----------------
cd '/home/omouse/cobaltserver/debug' && WANT_AUTOCONF_2_5="1"
WANT_AUTOMAKE_1_6="1" gmake -k -j1
compiling cserver.cpp (g++)
main.cpp:153: error: no matching function for call to
`std::list<userNode, std::allocator<userNode> >::insert(
std::_List_iterator<userNode, userNode&, userNode*>,
std::_List_iterator<userNode, userNode&, userNode*>&)'

/usr/include/c++/3.3.4/bits/list.tcc:88: error: candidates are:
std::_List_iterator<_Tp, _Tp&, _Tp*> std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_Tp, _Tp&, _Tp*>, const _Tp&) [with
_Tp = userNode, _Alloc = std::allocator<userNode>]

/usr/include/c++/3.3.4/bits/stl_list.h:831: error: void std::list<_Tp,
_Alloc>::insert(std::_List_iterator<_Tp, _Tp&, _Tp*>, unsigned int,
const _Tp&) [with _Tp = userNode, _Alloc = std::allocator<userNode>]

*** Exited with status: 2 ***
 
A

Andre Kostur

Hi, I just switched to using STL for my linked lists and obviously I
need a way to insert. I have all the necessary includes (list &
algorithm) and the other functions that I've used (erase & find) work.
But when I try to insert the iterator that was found, gcc 3.3.4 tells
me that I'm missing a few inputs. Anyway, here's the snippet of code.

main.cpp
-----------------
#include "usernode.h"
#include <list>
#include <algorithm>
...
userNode tempNode = userNode(user, pword, NULL, NULL, NULL, false,
NULL);
list<userNode>::iterator listNode;
listNode = find(usersOnline.begin(), usersOnline.end(), tempNode);
if (listNode != usersOnline.end())
{
usersOnline.insert(usersOnline.begin(), listNode);
}
...
-----------------

Not sure exactly what you're trying to accomplish. You appear to be
searching a list of userNodes for some value. And if you find it, add
another copy to the beginning of the list? If so.. shouldn't the second
parameter be "*listNode"? As in, you want to insert the object, not the
iterator....? (This seems odd... I would have expected if you _don't_
find it, insert a copy.....)
 
V

Victor Bazarov

OMouse said:
Hi, I just switched to using STL for my linked lists and obviously I
need a way to insert. I have all the necessary includes (list &
algorithm) and the other functions that I've used (erase & find) work.
But when I try to insert the iterator that was found, gcc 3.3.4 tells
me that I'm missing a few inputs. Anyway, here's the snippet of code.

main.cpp
-----------------
#include "usernode.h"
#include <list>
#include <algorithm>
...
userNode tempNode = userNode(user, pword, NULL, NULL, NULL, false,
NULL);
list<userNode>::iterator listNode;
listNode = find(usersOnline.begin(), usersOnline.end(), tempNode);
if (listNode != usersOnline.end())

Don't you mean

if (listNode == usersOnline.end())

? You only need to insert if you haven't found it in your list.

You can shorten the three lines by one:

{
usersOnline.insert(usersOnline.begin(), listNode);

Jus do

userOnline.push_front(tempNode);

or

userOnline.push_back(tempNode);

V
 
O

OMouse

Thanks for pointing that out. Here's the updated code:
-------------------------------
//Search the user database to verify username & password
userNode tempNode = userNode(user, pword, NULL, NULL, NULL, false,
NULL);
list<userNode>::iterator databaseNode = find(userDB.begin(),
userDB.end(), tempNode);
//And make sure the user isn't already logged in
list<userNode>::iterator onlineNode = find(usersOnline.begin(),
usersOnline.end(), tempNode);
if (databaseNode != usersOnline.end() && onlineNode ==
usersOnline.end())
{
usersOnline.insert(usersOnline.begin(), *databaseNode);
}
-------------------------------

It compiles now with the *databaseNode. I'm having an issue with gmake,
but that's something else.

Thanks for the help.
 
A

Andre Kostur

Thanks for pointing that out. Here's the updated code:
-------------------------------
//Search the user database to verify username & password
userNode tempNode = userNode(user, pword, NULL, NULL, NULL, false,
NULL);
list<userNode>::iterator databaseNode = find(userDB.begin(),
userDB.end(), tempNode);
//And make sure the user isn't already logged in
list<userNode>::iterator onlineNode = find(usersOnline.begin(),
usersOnline.end(), tempNode);
if (databaseNode != usersOnline.end() && onlineNode ==
usersOnline.end())
{
usersOnline.insert(usersOnline.begin(), *databaseNode);
}

Hmm... I wonder if std::list is the appropriate container in this case....
wouldn't a std::set make more sense?
 
O

OMouse

I just started using STL so I'm not very familiar with it. But from
what I just read about std::set, it seems to make more sense for
something like a command/function list.

Could you just explain how I would make sure that the key exists? Just
a simple != NULL?

Thanks,
OMouse
 
A

Andre Kostur

I just started using STL so I'm not very familiar with it. But from
what I just read about std::set, it seems to make more sense for
something like a command/function list.

A set is for when you have a bag of things. There may only be one of each
thing in the set.
Could you just explain how I would make sure that the key exists? Just
a simple != NULL?

There's a couple of ways:
1) same idea as you currently have, but use the .find member of std::set...
it's faster (generally speaking, I don't know how big your list/set is
going to get)
2) Try to insert the object into the set anyway, and check the return value
of the .insert call to see if the object was actually inserted (if you
care. If you simply care that after a call to insert, only 1 copy exists
in the set, simply insert the object into the set. It won't put in a
duplicate (it will basically ignore the second insert...) )

None of this has anything to do with pointers... so mentioning NULL makes
no sense....
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top