Connecting Tech Pros Worldwide Forums | Help | Site Map

STL list insertion compiler error

OMouse
Guest
 
Posts: n/a
#1: Aug 18 '05
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 ***
-----------------

Thanks for any help,
OMouse


Victor Bazarov
Guest
 
Posts: n/a
#2: Aug 18 '05

re: STL list insertion compiler error


OMouse wrote:[color=blue]
> 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())[/color]

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:

list<userNode>::iterator listNode = find ...; // always initialise!
if (listNode == usersOnline.end())
[color=blue]
> {
> usersOnline.insert(usersOnline.begin(), listNode);[/color]

Jus do

userOnline.push_front(tempNode);

or

userOnline.push_back(tempNode);
[color=blue]
> }
> ...[/color]

V
Andre Kostur
Guest
 
Posts: n/a
#3: Aug 18 '05

re: STL list insertion compiler error


"OMouse" <omouse@gmail.com> wrote in news:1124383002.075468.167850
@o13g2000cwo.googlegroups.com:
[color=blue]
> 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);
> }
> ...
> -----------------[/color]

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.....)
OMouse
Guest
 
Posts: n/a
#4: Aug 18 '05

re: STL list insertion compiler error


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.

Andre Kostur
Guest
 
Posts: n/a
#5: Aug 18 '05

re: STL list insertion compiler error


"OMouse" <omouse@gmail.com> wrote in news:1124386693.416904.226770
@g44g2000cwa.googlegroups.com:
[color=blue]
> 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.[/color]

Hmm... I wonder if std::list is the appropriate container in this case....
wouldn't a std::set make more sense?
OMouse
Guest
 
Posts: n/a
#6: Aug 18 '05

re: STL list insertion compiler error


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

Andre Kostur
Guest
 
Posts: n/a
#7: Aug 18 '05

re: STL list insertion compiler error


"OMouse" <omouse@gmail.com> wrote in news:1124400801.354415.95120
@f14g2000cwb.googlegroups.com:
[color=blue]
> 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.[/color]

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

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....

OMouse
Guest
 
Posts: n/a
#8: Aug 20 '05

re: STL list insertion compiler error


Ok, I suggested this to my dev team and I think I'll be converting the
code (again) to make it work with sets instead of lists.

Thanks again for the help,
OMouse

Closed Thread