?
Solved

Including Header Files - Urgent help needed

Posted on 2005-04-02
15
Medium Priority
?
213 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
  • 5
  • 3
  • 2
  • +2
12 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
Industry Leaders: 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!

 
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
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 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.
Suggested Courses

621 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