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 7 1867
"OMouse" <om****@gmail.com> wrote in news:1124383002.075468.167850
@o13g2000cwo.googlegroups.com: 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.....)
OMouse wrote: 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:
list<userNode>::iterator listNode = find ...; // always initialise!
if (listNode == usersOnline.end())
{ usersOnline.insert(usersOnline.begin(), listNode);
Jus do
userOnline.push_front(tempNode);
or
userOnline.push_back(tempNode);
} ...
V
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.
"OMouse" <om****@gmail.com> wrote in news:1124386693.416904.226770
@g44g2000cwa.googlegroups.com: 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.
Hmm... I wonder if std::list is the appropriate container in this case....
wouldn't a std::set make more sense?
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
"OMouse" <om****@gmail.com> wrote in news:1124400801.354415.95120
@f14g2000cwb.googlegroups.com: 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....
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jess Austin |
last post by:
hi,
I like the way that Python does lists, and I love the way it does
iterators. But I've decided I don't like what it does with iterators
of lists. Lists are supposed to be mutable sequences,...
|
by: Patrick Guio |
last post by:
Dear all,
I have some problem with insertion operator together with namespace.
I have a header file foo.h containing declaration of classes, typedefs and
insertion operators for the typedefs in...
|
by: John N. |
last post by:
Hi All,
Here I have a linked list each containing a char and is double linked.
Then I have a pointer to an item in that list which is the current
insertion point.
In this funtion, the user...
|
by: Kieran Simkin |
last post by:
Hi all,
I'm having some trouble with a linked list function and was wondering if
anyone could shed any light on it. Basically I have a singly-linked list
which stores pid numbers of a process's...
|
by: Robin Cole |
last post by:
I'd like a code review if anyone has the time. The code implements a basic
skip list library for generic use. I use the following header for debug
macros:
/*
public.h - Public declarations and...
| |
by: chellappa |
last post by:
hi
this simple sorting , but it not running...please correect error for
sorting using pointer or linked list sorting , i did value sorting in
linkedlist
please correct error
#include<stdio.h>...
|
by: Julia |
last post by:
I am trying to sort a linked list using insertion sort. I have seen a
lot of ways to get around this problem but no time-efficient and
space-efficient solution. This is what I have so far:
...
|
by: stmfc |
last post by:
list class is said to provide constant-time insertion and removal of elements at any location in the list.
but i cannot insert an element to an arbitrary position in the list using:...
|
by: AZRebelCowgirl73 |
last post by:
This is what I have so far:
My program!
import java.util.*;
import java.lang.*;
import java.io.*;
import ch06.lists.*;
public class UIandDB {
|
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...
|
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...
| |
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: 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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |