Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solving cross referencing

Tags:

c++

I have a problem creating some form of hierarchy with different object types. I have a class which has a member of another class, like this:

class A
{
public:
    A(){}
    ~A(){}

    void addB(B* dep){
        child = dep;
        dep->addOwner(this);
    }
    void updateChild(){
        child->printOwner();
    }
    void print(){
        printf("Printing...");
    }
private:
    B* child;
};

And this is class B:

class B
{
public:
    void addOwner(A* owner){
        ownerObject = owner;
    }

    //ISNT WORKING
    void printOwner(){
        ownerObject->print();
    }

private:
    A* ownerObject;
};

Calling a function of "B" out of class "A" works just fine but trying it vice versa gives a compiler error because A is not defined in B. It actually is by using an include and a forward declaration, but I guess its a cross reference problem which the compiler can not solve.

Is there any chance to solve this problem or should I rethink my design?

like image 767
SideEffect Avatar asked Sep 17 '10 12:09

SideEffect


People also ask

How do you fix cross referencing?

You can update cross-references manually by pressing Ctrl + A to select all and then pressing F9. Cross-references will also be updated when you switch to Print Preview or when you print (if the Word option Update fields before printing is turned on).

What is an example of cross referencing?

Cross references are document elements that point to a different element in the same document. For example, a cross reference can point to a different page in the document (e.g. “see page 13”), to a footnote (e.g. “see note 2 on page 13”) or to a specific heading (e.g. “see heading 3.1: The Hobbit”), among others.


1 Answers

You say that you already solved your circular dependency problem by using a forward declaration of A instead of including the header where A is defined, so you already know how to avoid circular includes. However, you should be aware of what is possible and what is not with incomplete types (i.e. types that have been forward declared).

In your case, you try to call the member function print on an object that has an incomplete type; the compiler knows nothing about this type excepts that it will be defined at some point, so it does not allow you to do this. The solution is to remove the implementation of the printOwner member function from the B header and put it into an implementation file:

//B.hpp

class A; // forward declaration

class B
{
  public:
    void addOwner(A* owner);

    void printOwner() const; // I think this member function could be const

  private:
    A* ownerObject;
};

//B.cpp

#include "B.hpp"
#include "A.hpp" // here we "import" the definition of A

void B::addOwner(A * owner)
{
    ownerObject = owner;
}

void B::printOwner() const
{
    ownerObject->print(); //A is complete now, so we can use its member functions
}

You could possibly do the same thing in the A header.

like image 176
Luc Touraille Avatar answered Nov 03 '22 10:11

Luc Touraille