Solved

Lists

Posted on 2000-03-26
9
228 Views
Last Modified: 2010-04-10
Hello. This is my problem:

I have defined a class which is a list: CList. The list is composed by elements of another class: CEnsemble.
OK, here is the code of the header file:

class CEnsemble
{
protected:
        CString Name;
      CEnsemble *nextNode;
        int number;
public:
      CEnsemble(CString);                  void WriteName (CString);      CString ReadName ();                  CEnsemble* GetNextPtr();      void SetNextPtr (CEnsemble*);      void SetNumber (int);
      int GetNumber();
};


class CListe: public CDialog
{
protected:
      CEnsemble* listHead;                  int NoOfNodes;            
public:
      CListe();                        void displayList();
      void deleteList();
        void InsertList(CString);             int GetNoOfNodes();
      CEnsemble* SearchList(int);
};

Now, I'd like to insert a list (CList) as a private property of CEnsemble. The problem is that the definition of CList is before the definition of CList and then it will not recognize the CList class. What can I do???

Thanks in advance.
0
Comment
Question by:javigil1
  • 4
  • 3
  • 2
9 Comments
 

Expert Comment

by:LazyNerdEE
ID: 2658037
Just do a forward declaration of the class CList before the definition of the CEnsemble class as follows :

class CList;

Hope this helps,
Lazynerd.
0
 

Author Comment

by:javigil1
ID: 2658373
Upssss! Doesnt work:

So, my code would be then:

class CListe; //forward declaration. New line

class CEnsemble: public CObject
{
protected:
  CString Name;
  CEnsemble *nextNode;
  int number;
  CListe SCListe;   //New line
....

class CListe: public CDialog
{
protected:
  CEnsemble* listHead;
  int NoOfNodes;            
public:
  CListe();      //Constructor
  void displayList();
  .......


And I get this:
Compiler Error C2079
'SCListe' uses undefined class 'CListe'

0
 

Author Comment

by:javigil1
ID: 2658726
Adjusted points from 125 to 145
0
 

Author Comment

by:javigil1
ID: 2658727
I will try to make the whole question again:

I need help!!!

I have a list:  CListe

Code of CListe:

class CListe: public CDialog
{
protected:
     CEnsemble* listHead;
     int NoOfNodes;            
public:
     CListe();                                
     void InsertList(CString);      
     CEnsemble* SearchList(int);
};

As you can see, the members of the list are
objects from the class CEnsemble, which is so
defined:

class CEnsemble: public CObject
{
protected:
     CString Name;
     CEnsemble *nextNode;
     CListe SCList();       // <--Important!!! (i'm not sure)
public:
     CEnsemble(CString);
     void WriteName (CString);
     String ReadName ();
     CEnsemble* GetNextPtr();
     void SetNextPtr (CEnsemble*);
};

Well, in each object there's another list (SCList).
Get the idea?? A list with objects. In each object
some variables and another list.
Since CListe is declared after CEnsemble, first of
all I made a forward declaration, so that the order is:

First:
class CListe;  //forward declaration (we use it in CEnsemble)
Second:
class CEnsemble: public CObject
{
protected:
........
Third:
class CListe: public CDialog
{
protected:
......

Now I want to use the whole structure, with a code like this:

CListe lista1;
element=lista1.SearchList(1);  //I get an element of the list
element->SCListe.init();

........and the last line doesn't work. But I dont know why.
I have also tried:
      element->SCListe->init();
but i dont get anything...

Can you help me????   PLEASE!!

Thanks in advance...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
feenix earned 145 total points
ID: 2658871
One way to make it work would be to use a dynamic object and not a static one. For example:

Class CListe;

Class CEnsebmle
{
....
  CListe *SCList;
....
};

Class CListe
{
....
};

And then just initialize the SCList in the CEnsebmle's constructor. This way it will certainly work. I have never even tried to use static objects in what you're trying to do, I think it isn't even possible.
0
 

Author Comment

by:javigil1
ID: 2659002
Doesnt work:

I try to do what you say. Then I initialize  my list in
the constructor of CEnsemble, as you say:

CEnsemble::CEnsemble(CString name){    //contructor
      Name=name;
      SCList->init();
}

....I use the method init:

void CListe::init(){
      listHead = NULL;
      NoOfNodes=0;
}


but when I try to make this initialization, the program terminates and I get an error: Close/Debug/Details

0
 
LVL 4

Expert Comment

by:feenix
ID: 2660427
You did remember to create the object before trying to use it?

SCList = new CListe;
0
 

Expert Comment

by:LazyNerdEE
ID: 2661125
Try doing the same forward declaration as I said before and use a pointer to CList instead of an object of CList(Would it work?! I hope so.)
0
 

Expert Comment

by:LazyNerdEE
ID: 2661135
Oops. Sorry for my previous comment. Pls disregard it. Still my forward declaration answer holds good right?:-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now