Solved

Lists

Posted on 2000-03-26
9
234 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

615 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