Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Lists

Posted on 2000-03-26
9
231 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

839 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