• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

CArray - passing as parameter!

Hi I am using VC++ 6.

I have two CArray declared as

      CArray <CellNode, CellNode> GridCells;
      CArray <CBug, CBug> BugArray;

CellNode is a struct in class CMyClass and CBug is another class.
I am having a problem with a method in CBug which I pass GridCells declared above.  The method is declared as:

      void MoveBug(CArray <CellNode, CellNode>&);

The problem is that CellNode struct is part of and declared in CMyClass
and the method in CBug above seems to need it because of the template CArray passed.  I cannot declare  another CellNode as that will not work and I cannot take CellNode declaration out of CMyClass (that is where it belongs).

How can this be solved?

ps.  CBug is included in CMyClass as #include "Bug.h" and including CMyClass in CBug does not seem to work

Bear with me I am pretty new to this.
1 Solution
In your CBug class you will need to refer to CellNode as CMyClass::CellNode.

gbzhhuAuthor Commented:
but CBug class does not know the existance of CMyClass.  You can't have CMyClass::CellNode in CBug unless CMyClass was included in CBug
Does bug.h have to be included in the CMyClass header file? Could you avoid this with a forward declaration?
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.

You mentioned you were new to this, so just in case:

a forward declaration for a class looks like this:

class CBug;

You can get away with this in your header file as long as you don't use any of the functionality (like calling a member function of the CBug class) of the CBug class *in the header file itself*.
gbzhhuAuthor Commented:
Thank you chaps.

Forward declaration will not help as I actually construct objects of CBug in CMyClass ie.

CBug aBug();

This will not work for forward declaration.
As this is a catch 22 situation I have rearranged my design and solved the problem ( I added a helper class which is connected to both CBug and CMyCalss.

I will now delete the question.  should you feel your answer solved the problem and I unfairly deleted the question, please put a comment

gbzhhuAuthor Commented:
This question has a deletion request Pending
This question no longer is pending deletion
I'm not going for points here, this is just fyi.

You can forward declare CBug in the header file of CMyClass and then include bug.h in the implementation file of CMyClass.

As long as you don't actually instantiate or use a CBug object in the header file of CMyClass this should avoid the catch22 situation.
I think you should declare CellNode struct in other golbal file and then include it in both CBug.h and MyClass.h
Hope this help!
gbzhhuAuthor Commented:
Thank you both mnewton and thuannc for the comments.

I have already done a lot of change to my code (redesigned as I said above) and it seems to work fine.

The question should be pending deletion!! as no answer helped me solve it, though I learned more about forward declaration.

I will leave it to customer service to decide whether it is ethical to delete it or not.
gbzhhuAuthor Commented:
This was as forced answer, I have already deleted the question and solved my problem by myself.  You said you were not going for points, but i could not delete the question after you answered, so I had to accept your answer.  Well.... it is only 50 points.  I knew forward declaration ( I am new to C++ but that new - forward declaration is basic stuff)

Thanks by the way.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now