Two Classes That Refer to Each Other

Two classes that refer to each other

You can use forward declarations in the header files to get around the circular dependencies as long as you don't have implementation dependencies in the headers. In Window.h, add this line:

class App;

In App.h, add this line:

class Window;

Add these lines before the class definitions.

Then in the source files, you include the headers for the actual class definitions.

If your class definitions reference members of the other class (for example, in inlines), then they need to be moved to the source file (no longer inline).

Two classes that refer to each other in their constructor

Since vec2::vec2(vec3 V) is implemented in the .h file, make it inline.

inline vec2::vec2(vec3 V)
{
x = V.x;
y = V.y;
}

Otherwise, every .cpp file that includes the .h file has a non-inline implementation of the constructor. That causes the problem reported by the linker.

The other option will be to move the definition to a .cpp file (and not use inline).

two classes referencing each other

You have already added the forward declaration to item.h so all you need to do is add the following line to item.cpp.

#include "container.h"

container.h already includes item.h so you don't have to make any additional changes but as Mahmoud Fayez pointed out you can add a forward declaration there as well. This will remove the dependency on the header file which is typically desired - it can reduce build times on large projects and allows the header file to "stand on it's own".

How do I create two classes that refer to each other in different header files?

Since you've forward declared the class Organization in Seller.h there is no need to write #include "Organisation.h". Similarly, in Organization.h" since you've forward declared class Seller there is no need to write #include "Seller.h". Also, always take into account cyclic dependency like in your program Organization.h has a #include "Seller.h" and then Seller.h has a #include "Organization.h"

The running(successfully compiled) program can be seen here.

Organization.h

#ifndef ORGANIZATION_OOP3_H
#define ORGANIZATION_OOP3_H
//#include "Seller.h"
#include <vector>
class Seller;
class Organization{
std::vector<Seller*> own;

};
#endif

Seller.h

#ifndef OOP_3_SELLER_H
#define OOP_3_SELLER_H
//#include "Organization.h"
class Organization;
class Seller{
protected:
Organization*owner;

};
#endif

In the .cpp files(also called source files) you should include the headers using #include.

Your program has other problems too like many(and i mean many many)of the methods in Seller.h have no return value for methods that have non-void return type.

I tried solving some of the problems in you github code but there are just too many problems(errors and warnings) to solve. Also i would suggest you to use cpp files as well.

python classes that refer to each other

In python, the code in a class is run when the class is loaded.

Now, what the hell does that mean? ;-)

Consider the following code:

class x:
print "hello"
def __init__(self): print "hello again"

When you load the module that contains the code, python will print hello. Whenever you create an x, python will print hello again.

You can think of def __init__(self): ... as equivalent with __init__ = lambda self: ..., except none of the python lambda restrictions apply. That is, def is an assignment, which might explain why code outside methods but not inside methods is run.

When your code says

class X(models.Model):
creator = Registry()
creator.register(Y)

You refer to Y when the module is loaded, before Y has a value. You can think of class X as an assignment (but I can't remember the syntax for creating anonymous classes off-hand; maybe it's an invocation of type?)

What you may want to do is this:

class X(models.Model):
pass
class Y(models.Model):
foo = something_that_uses_(X)
X.bar = something_which_uses(Y)

That is, create the class attributes of X which reference Y after Y is created. Or vice versa: create Y first, then X, then the attributes of Y which depend on X, if that's easier.

Hope this helps :)

Two classes having references to each other

I fixed this when moving both the Tkinter GUI class and the controller class to their own .py file instead of having both in the same file.

I found that I forgot some self.'s so the controller would try to acces the GUI object defined in the global scope instead of the reference I gave. After placing those self.'s all is working as I expected!

So for other people:
If you want to give two objects a reference to each other:

  1. Set a placeholder for the other object in each constructor. (thanks @Dashadower)
  2. Give each class a method to reassign that placeholder with the actual reference
    (Give the object as an argument)

  3. Dont forget to call the objects reference of the other object. (USE self.)

Making two classes reference eachother

You can just have a dictionary that holds the references, and call the methods straight from the Manager (which shouldn't really be named Manager, as it does not serve that purpose now) instance.

class Home(object):
pass

class Club(object):
pass

PLACES = {
'home': Home(),
'club': Club()
}

class Manager(object):
def display_plot_and_get_option(self):
return raw_input('Where do you want to go?')
def get_next_place(self, place_name):
return PLACES[place_name]

m = Manager()
while 1:
place_name = m.display_plot_and_get_option()
m.get_next_place(place_name)

How to represent 2 classes with references to each other in UML?

No. That's plain wrong. Composite aggregation can only be on one side. (Imagine the car/wheel example: each aggregating the other is nonsense.) Remove the diamonds and you're done.

You can go further and put dots instead of the diamonds. That will mean that both sides have attributes referring the class at the other side. See this answer.

Sample Image



Related Topics



Leave a reply



Submit