473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

CList double element error...

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;
}
}

Jul 23 '05 #1
7 3803
Arcane wrote:
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:
----------------

CList<CASystem, CASystem> AllCAS;
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
Jul 23 '05 #2
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.

Jul 23 '05 #3
>>CList<CASystem, CASystem> AllCAS;
CList is defines as
template< class TYPE, class ARG_TYPE > class CList : public CObject
You are passing CASystem for ARG_TYPE
pCAS = new CASystem;
pCAS->ID = ID;
AllCAS.AddHead(pCAS);

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

Jul 23 '05 #4
Arcane wrote:
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
Jul 23 '05 #5
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...

Jul 23 '05 #6
Arcane wrote:

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;
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #7
Karl Heinz Buchegger wrote:
Arcane wrote:
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 );


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
Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ellie O'Donnell | last post by:
Hi, I wrote a DB2 stored procedure in COBOL, and now I need to prepare a CLIST in order to call it. This CLIST would be for people who want to use the stored procedure outside of COBOL or Pl/I....
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
1
by: Corey2004 | last post by:
I am importing a visual 6 C++ dll into C#. The methods in the dll take a CList type and I was wondering what type in C# can I use that correctly matches the CList type. Is the ArrayList type in C#...
5
by: hello | last post by:
How can I define the schema so that myage element has to be double or null? <xs:simpleType name="myage"> <xs:restriction base="xs:double"> <xs:enumeration value="null"/> </xs:restriction>...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
1
bajajv
by: bajajv | last post by:
Hi, While implementing CList class, I got linker errors for constructor and destructor. Below is the code - template<class TYPE, class ARG_TYPE = const TYPE&> class CList { protected: struct...
1
bajajv
by: bajajv | last post by:
Hi, What is the difference between CObList and CList? If we use a CList as - CList<CObject*, CObject*>, isnt it similar to CObList?
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
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...
0
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,...
0
isladogs
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 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.