0

How can I properly reference child and parent objects within both objects (doubly-linked child and parent)? When doing that, I get a compilation error: **** does not name a type. I suspect it has to do with the #include statements being omitted due to the #define tags. How should these tags be hence included?

The three files (Parent.h, Child.h, main.cpp) written as such:

    /* Parent.h */
#pragma once
#ifndef _CHILD_CLS
#define _CHILD_CLS

#include "Child.h"

class Parent {
public:
    Parent() {}
    ~Parent() {}
    void do_parent(Child* arg);
};
#endif

/* Child.h */
#pragma once
#ifndef _CHILD_CLS
#define _CHILD_CLS

#include "Parent.h"

class Child {
public:
    Child() {}
    ~Child() {}
    void do_child(Parent* arg);
};
#endif

/* main.cpp */
#include "child.h"
#include "parent.h"

int main()
{
    Child   a();
    Parent  b();
    a.do_parent(Child& arg);
    return 0;
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
jhtong
  • 1,529
  • 4
  • 23
  • 34

5 Answers5

1

You have a circular dependency of header files. Just forward declare either of the classes in one of the headers. For example:

    /* Parent.h */
#pragma once
#ifndef _CHILD_CLS
#define _CHILD_CLS

//#include "Child.h"   ----> Remove it
class Child;           //Forward declare it

class Parent {
public:
    Parent() {}
    ~Parent() {}
    void do_parent(Child* arg);
};
#endif
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks. How should that be done, e.g. do I declare #include "Parent.h" in the Child header, and #include "Child.h" in the Parent header? – jhtong Feb 11 '13 at 09:04
  • @toiletfreak: I already showed the code example of how to do it. – Alok Save Feb 11 '13 at 09:06
  • I have tried to place an #include in Parent.h, but it still gives an error of `does not name a type`. – jhtong Feb 11 '13 at 09:06
  • If you see the code properly you can see I removed `"Child.h"` from being included in `"Parent.h"` and added a forward declaration `class Child;` instead. – Alok Save Feb 11 '13 at 09:07
1

Use class prototypes / forward declarations:

class Child;

and

class Parent;

before each others class declaration and remove the includes.

drahnr
  • 6,782
  • 5
  • 48
  • 75
1

You defined two functions instead of objects, see most vexing parse

Update

Child   a();              // a is a function returns Child object
Parent  b();              // b is a function returns Parent object
a.do_parent(Child& arg);

TO

Child   a;           // a is a Child object
Parent  b;           // b is a Parent object
b.do_parent(&a);

Also, you have circular include issue, to break circular include, you need to forward one type :

Child.h

#pragma once
#ifndef _CHILD_CLS
#define _CHILD_CLS

//#include "Parent.h"  Don't include Parent.h
class Parent;           // forward declaration
class Child {
public:
    Child() {}
    ~Child() {}
    void do_child(Parent* arg);
};
#endif

Child.cpp

#include "Parent.h"
// blah
billz
  • 44,644
  • 9
  • 83
  • 100
  • Gee! Thanks. That worked. However, if an instance of Child was in Parent, is there any way (apart from pointers) that this Child instance can be accessed? – jhtong Feb 11 '13 at 09:20
  • Child can be a member of Parent but you still need to be careful about circular include issue :) – billz Feb 11 '13 at 09:35
0

The error, I guess (you should always include the complete and unedited error message when asking about compilation/linker errors), is this line:

a.do_parent(Child& arg);

You should pass a pointer to a Child object here, not a variable declaration:

b.do_parent(&a);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

You need to have a forward declaration of class Parent or class Child. Try modifying one of your files.

In you Parent.h:

#pragma once
#ifndef _CHILD_CLS
#define _CHILD_CLS

class Child;     // Forward declaration of class Child

class Parent {
public:
    Parent() {}
    ~Parent() {}
    void do_parent(Child* arg);
};
#endif

Or in your Child.h:

#pragma once
#ifndef _CHILD_CLS
#define _CHILD_CLS

class Parent;    // Forward declaration of class Parent

class Child {
public:
    Child() {}
    ~Child() {}
    void do_child(Parent* arg);
};
#endif

This question should help you a lot.

Community
  • 1
  • 1
Mark Garcia
  • 17,424
  • 4
  • 58
  • 94