?
Solved

Including Header Files - Urgent help needed

Posted on 2005-04-02
15
Medium Priority
?
210 Views
Last Modified: 2013-11-18
Hi,

I have a main class, MainClass.h, and another class, Dog.h.  MainClass.h has included Dog.h (#include "Dog.h") and creates instances of that class.

My problem is that i want to be able to access the MainClass object from the Dog, but i dont want to inheirited.  I want to store a pointer to MainClass inside Dog, but when i do this i get errors.

If i remove the #include "Dog.h" in the MainClass it compiles ok.  I think it is because if there are the 2 includes in both the classes, then there is some kind of problem.

I am using #ifndef and #define but it still doesnt resolve my issue.

How can i have a pointer to the MainClass object from the Dog class?

Thanks,
Michael
0
Comment
Question by:Xavior2K3
[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
  • 5
  • 3
  • 2
  • +2
15 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 672 total points
ID: 13689459
First ensure you have in all your header files "header guards". Something like:

#ifndef DOG_H
#define DOG_H
// ALL your header file here
#endif

Second, to avoid circular reference, you can make an advanced declaration somewhere. Somethink like:

class Dog;   // Just report there is a class called Dog




0
 
LVL 1

Author Comment

by:Xavior2K3
ID: 13689527
Thanks for the quick response.  It worked fine, but as soon as i tried to use the pointer, it said:

error C2027: use of undefined type 'MainClass'
error C2227: left of '->displayHeader' must point to class/struct/union type is ''unknown-type''
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13689603
Hi Xavior2K3,
When you have circular dependencies in classes, you can't use inline methods which dereference the forward-declared class, since it's still incomplete (the compiler knows such a class exists, but doesn't know any details). You can only define pointers, and can't do anything with them yet.

Use just a method declaration and implement the method seperately in a .cpp file.

Cheers!

Stefan
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:stefan73
ID: 13689612
example:

// header

class B;

class A {
    B* myB;
};

class B{
    A* myA;
};

this works fine so far. But you can't do inline actions involving incomplete member myB:

class A {
    B* myB;
    void do_something_with_B() { B->some_method(); }
};

What you can do instead:

// header

class B;

class A {
    B* myB;
    void do_something_with_B();
};

class B{
    A* myA;
};

// implementation.cpp

#include "header"

void A::do_something_with_B() {
    B->some_method();
}


...but this isn't inline anymore.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13689624
Oops, typo: it's not
B->some_method()
..but of course
myB->some_method()

...in all cases.
0
 
LVL 1

Author Comment

by:Xavior2K3
ID: 13689666
Im still a little confused. What i want is:

ClassA - Has variables and methods i want to be able to access from the BaseClass
BaseClass - Dog and Cat inherit from this class, use its methods.

And ClassA contains 2 Dog objects and a Cat.

From the Dog or Cat i want to beable to change a public variable in ClassA.  This is just one variable so if it is changed in Dog it is changed in Cat as well.

Sorry for the confusion but if i can understand this problem then ill have it :)

Thanks
0
 
LVL 1

Author Comment

by:Xavior2K3
ID: 13689864
Ok i have seperated the class and the function definitions are in the CPP file, but i am getting a "use of undefined type" error when i try and use the pointer.  How could this happen?
0
 
LVL 3

Expert Comment

by:ghimireniraj
ID: 13690653
From what I see:

Derive CLASSA from DOG AND CAT;
That makes BASEclass a parent of classA;

use virtual function and dynamically cast pointers to access a classA member fron baseclass;
check and see



0
 
LVL 3

Assisted Solution

by:ghimireniraj
ghimireniraj earned 664 total points
ID: 13690657
From what I see:

Derive CLASSA from DOG AND CAT;
That makes BASEclass a parent of classA;

use virtual function and dynamically cast pointers to access a classA member fron baseclass pointer;
check and see
0
 
LVL 12

Assisted Solution

by:stefan73
stefan73 earned 664 total points
ID: 13691140
I don't think multiple inheritance will solve the problem. There are almost no relevant uses for it.

> ClassA - Has variables and methods i want to be able to access from the BaseClass
> BaseClass - Dog and Cat inherit from this class, use its methods.

> And ClassA contains 2 Dog objects and a Cat.

> From the Dog or Cat i want to beable to change a public variable in ClassA.  This is just one variable so if it is changed in Dog it is changed in Cat as > well.

If you want to have a variable which affects both Dogs and Cats, use a static variable in the base class. To use it within ClassA, make the static variable protected, and have a public get/set method (also static).
0
 
LVL 12

Expert Comment

by:stefan73
ID: 13691149
The best thing is to think where your public variable should be located from the OO model point of view. If it's something common for dogs and cats (i.e., number of legs), then it should go into the base class. "ClassA" is a poor naming for a class, use a name which belongs to your model world. If you can't find a name, the class is probably superflous.
0
 
LVL 3

Expert Comment

by:CmdrRickHunter
ID: 13693547
/// BaseClass.h
#ifndef BASECLASS_H
#define BASECLASS_H

classA; // prototype for class... lets it know that there's a class named ClassA

class BaseClass
{
   // function prototypes here
}

class Dog : public BaseClass
{
  // function prototypes here
}

class Cat :public BaseClass
{
  //
}

#endif

//// classA.h
#ifndef CLASSA_H
#define CLASSA_H

class Dog;
class Cat;

class ClassA
{
   Dog    dogs[2]; // arrays are like pointers
   Cat*     cat;
};




then, in the implemtnation (cpp) files, you can #include"classA.h" and #include"BaseClass.h" and have everything work out fine, because once both files are included, you have access to everything you could ever want.

prototyping of classes is used when the compiler needs to know "we will have a class named this".  You're limited in what you can do while a class is prototyped.  You can use pointers to objects of that type, but you cannot create the objects themselves (because we dont know whats inside them).  You also can't call member functions since we don't know what they are yet.

I second stefan73's comment about the OO stuff.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 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