CList double element error...

A

Arcane

I'm having trouble workin with a simple CList:

the function always adds the first element twice and I don't know how.
also, the later added elements do not appear immediately in the list,
but only on the next function call...
here is the code:
----------------

CList<CASystem, CASystem> AllCAS;

CASystem *GetCreateCASystem(int ID)
{
bool bFound = false;
CASystem *pCAS = NULL;
// check if AS list exists
if(AllAS.IsEmpty())
{
// create new AS
pCAS = new CASystem;
pCAS->ID = ID;
AllCAS.AddHead(pCAS);
return pCAS;
}
else
{
// search for CAS
int i = 0;
POSITION pos = AllCAS.GetHeadPosition();
pCAS = AllCAS.GetHead();
while(i<AllCAS.GetCount())
{
if(pCAS->ID == ID)
{
// CAS already saved
bFound = true;
break;
}
else
{
i++;
pCAS = AllCAS.GetNext(pos);
}
}
// if no CAS yet, create CAS
if(!bFound)
{
// set new CAS and return it
pCAS = new CASystem;
pCAS->ID = ID;
AllCAS.AddTail(pCAS);
}
return pCAS;
}
}
 
V

Victor Bazarov

Arcane said:
I'm having trouble workin with a simple CList:

What's a "CList"? Is that your class? If it's part of some package,
you might be better off asking in a newsgroup dedicated to that package.
the function always adds the first element twice and I don't know how.
also, the later added elements do not appear immediately in the list,
but only on the next function call...
here is the code:

So, CList is a template, eh? Does it store objects?
CASystem *GetCreateCASystem(int ID)
{
bool bFound = false;
CASystem *pCAS = NULL;
// check if AS list exists
if(AllAS.IsEmpty())
{
// create new AS
pCAS = new CASystem;
pCAS->ID = ID;
AllCAS.AddHead(pCAS);

I am trying to understand the semantics of CList. You seem to be
inserting (adding) a _pointer_ here. Does CList store pointers? Or
does its 'AddHead' member function just takes a pointer and stores
an object?
return pCAS;
}
else
{
// search for CAS
int i = 0;
POSITION pos = AllCAS.GetHeadPosition();
pCAS = AllCAS.GetHead();
while(i<AllCAS.GetCount())
{
if(pCAS->ID == ID)
{
// CAS already saved
bFound = true;
break;
}
else
{
i++;
pCAS = AllCAS.GetNext(pos);

Here you're asking for a head again. 'pos' doesn't seem to change
at all...
}
}
// if no CAS yet, create CAS
if(!bFound)
{
// set new CAS and return it
pCAS = new CASystem;
pCAS->ID = ID;
AllCAS.AddTail(pCAS);
}
return pCAS;
}
}

I recommend you find a decent pencil and a good piece of paper and
"step" through your code trying to understand how the variables you
declared, change, and how their new values are used.

V
 
A

Arcane

CList ist one of the basic MFC classes in C++, basically a list
implementation. it work with all kinds of elements, pointers as well.
the problem is as described that the first element is added twice, and
I'm wondering why.
 
U

upashu2

CList<CASystem, CASystem> AllCAS;
CList is defines as
AddHead is defined as
POSITION AddHead( ARG_TYPE newElement );
Here u are passing CASystem* as ARG_TYPE parameter.
Did u get it?

If you want to allocate the object (say of Type T) on heap, then make
ur CList decleration as
CList<CASystem, CASystem* > AllCAS;
first template parameter will telll the type of object u r storing and
second parameter will tell how u r storing, here CList will keep the
pointers to CASystem objects already allocated by you.

You also can declare your CList as CList<CASystem, CASystem> AllCAS;
But don't pass pointers to AddHead() or AddTail(), Instead pass objects
itself.
CASstem ca;
ca.ID = ID;
AllCAS.AddHead(ca); ///pass by value
 
V

Victor Bazarov

Arcane said:
CList ist one of the basic MFC classes in C++

There are no "MFC classes in C++". There are MFC classes in Visual C++,
and if you need help with any of those (which you seem to, judging by your
re-post on the same topic _after_ I've given you some information that at
least should get you to begin looking at your own code and at the MFC
documentation), go to

microsoft.public.vc.mfc

newsgroup. Here we help with, and recommend, using std::list. You might
want to take a better look at standard containers instead of that aging
library.
>, basically a list
implementation. it work with all kinds of elements, pointers as well.
the problem is as described that the first element is added twice, and
I'm wondering why.

V
 
A

Arcane

this is not about how to store it. no matter if I'm using pointers or
objects, I always have a double element in it...
 
K

Karl Heinz Buchegger

Arcane said:
this is not about how to store it. no matter if I'm using pointers or
objects, I always have a double element in it...

This is probably because you insert it twice.
Fire up your debugger, step through the code and
eventually you will figure out why it happend.

BTW: The way to iterate through an MFC list is like this:

POSITION pos= list.GetHeadPosition();
while( pos ) {
pCAS = list.GetNext( Pos );
// do something with pCAS
}

No need for that mumbo jumbo you do with GetHead(), GetCount() and
the counter variable i;
 
V

Victor Bazarov

Karl said:
This is probably because you insert it twice.
Fire up your debugger, step through the code and
eventually you will figure out why it happend.

BTW: The way to iterate through an MFC list is like this:

POSITION pos= list.GetHeadPosition();
while( pos ) {
pCAS = list.GetNext( Pos );

Uh... C++ is case-sensitive, IIRC. Did you mean to say

pCAS = list.GetNext(pos);

?
// do something with pCAS
}

No need for that mumbo jumbo you do with GetHead(), GetCount() and
the counter variable i;

V
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top