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 *GetCreateCASys tem(int ID)
{
bool bFound = false;
CASystem *pCAS = NULL;
// check if AS list exists
if(AllAS.IsEmpt y())
{
// create new AS
pCAS = new CASystem;
pCAS->ID = ID;
AllCAS.AddHead( pCAS);
return pCAS;
}
else
{
// search for CAS
int i = 0;
POSITION pos = AllCAS.GetHeadP osition();
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;
}
} 7 3815
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 *GetCreateCASys tem(int ID) { bool bFound = false; CASystem *pCAS = NULL; // check if AS list exists if(AllAS.IsEmpt y()) { // 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.GetHeadP osition(); 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
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.
>>CList<CASyste m, 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
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.publi c.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
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...
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.GetHeadPos ition();
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
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.GetHeadPos ition(); while( pos ) { pCAS = list.GetNext( Pos );
Uh... C++ is case-sensitive, IIRC. Did you mean to say
pCAS = list.GetNext(po s);
?
// do something with pCAS }
No need for that mumbo jumbo you do with GetHead(), GetCount() and the counter variable i;
V This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. I am not familiar
with CLISTS and don't know where to begin. My stored proc has a long
input/output parameter list. I don't see anything on CLISTs in the IBM
manual. Help!
Thanks!
|
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;
};
struct pcb *pcb_pointer;
|
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# comparable or should I just create my own List type in C#?
|
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>
</xs:simpleType>
</xs:schema>
|
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????
| |
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 would be fairly easy to add these different double arrays
to different ArrayLists and then use ToArray to get all the numbers back
in one array. Sort of a cheap concatenate so I don't need to loop
through all the points or keep newing larger arrays
|
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 bug , i am still c++ beginner
|
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 CNode
{
CNode* pNext;
|
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?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |