Solved

Lists

Posted on 2000-03-26
9
227 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
Comment Utility
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
Comment Utility
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
Comment Utility
Adjusted points from 125 to 145
0
 

Author Comment

by:javigil1
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 4

Accepted Solution

by:
feenix earned 145 total points
Comment Utility
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
Comment Utility
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
Comment Utility
You did remember to create the object before trying to use it?

SCList = new CListe;
0
 

Expert Comment

by:LazyNerdEE
Comment Utility
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
Comment Utility
Oops. Sorry for my previous comment. Pls disregard it. Still my forward declaration answer holds good right?:-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

10 Experts available now in Live!

Get 1:1 Help Now