Multiple files -- cyclic dependancy and linker errors (newbie)

B

BWIGLEY

I'm trying to make a game and it's my first complicated project in c++
that uses multiple files, I've come up with a couple of errors and I
really don't have a clue. So if you could please take a look at my
game, I'd
really appreciate it (about 90Kb):
http://homepages.ihug.co.nz/~bwigley/Matamadia.zip

The first problem is what I think is a cyclic dependancy: display.h
and level.h include each other, and I don't know how to fix this.

The next problem (once the cyclic dependancies are fixed) is linker
errors:
eg. level.cpp refrences dsply.clean(), but the object dsply is created
by main.cpp.

I'm making it in dev-c++ too so I've included the project file for if
you have dev-c++. Dev-c++ uses g++ so there is a makefile for it
there in the file.

If you see any other obvious problems/stupidities please tell me
too...

My program uses the non-standard "pdcurses" for cursor positioning,
hopefully it will compile fine under windows but it won't compile in
it's current state under any other OS. But all refrences to curses.h
are in display.cpp (and -lpdcurses in the makefile), so for the
purposes of finding errors, references to curses could be easily
commented out (or if you really wanted too, you could replace it with
standard i/o, I've written basic instructions to change it in the file
and it wouldn't be too hard to do but it would probably run slowly and
wouldn't be worth the effort of helping me ;)).
 
G

Gianni Mariani

BWIGLEY said:
I'm trying to make a game and it's my first complicated project in c++
that uses multiple files, I've come up with a couple of errors and I
really don't have a clue. So if you could please take a look at my
game, I'd
really appreciate it (about 90Kb):
http://homepages.ihug.co.nz/~bwigley/Matamadia.zip

The first problem is what I think is a cyclic dependancy: display.h
and level.h include each other, and I don't know how to fix this.

You don't do that.

Consider it a design issue and if you interface (for whatever reason)
needs to do this, it is a bad design.

BTW - I have not looked at your header files.
 
M

Michael

I'm trying to make a game and it's my first complicated project in c++
that uses multiple files, I've come up with a couple of errors and I
really don't have a clue. So if you could please take a look at my
game, I'd
really appreciate it (about 90Kb):http://homepages.ihug.co.nz/~bwigley/Matamadia.zip

The first problem is what I think is a cyclic dependancy: display.h
and level.h include each other, and I don't know how to fix this.

A minimal fix would be to get rid of both #includes in level.h - it
doesn't need them.

A better solution would be to create a separate header for each class
and struct - I know, it's a pain in the neck, but that will give you
the fine-grained control you need. (This is not general advice for
all developers at all levels of sophistication, but it will likely
help you in the current case. In other words, the exact rule for what
to put in which header file is mushier, but what I've suggested will
have you err on the side of caution.) Given that, be very careful if
you #include a header file in another header file. You rarely need to
do it. For example, suppose you have a class like this:

class A : public B {
C c;
D* d;

public:
E myFunction(F f);
G* myOtherFunction(H* h);
}

In this case, you'll need to #include B.h, since A derives from B, and
C.h, because A contains a struct/class of type C.

You will not need to #include D.h, because you're only using a pointer
to it. Instead, you can put a forward declaration like this before
your class:

class D; // <- this is a forward declaration

You do not need to #include E.h, F.h, G.h, or H.h, because they are
only used in function signatures. You can forward declare them as
well. (This is a commonly misunderstood point, so expect someone to
tell you, incorrectly, that yes you do need to #include E.h and F.h.)

So basically, split your header files by class, then forward declare
anything you don't actually need, but then #include all the relevant
the .h files in the .cpp files. I think that will solve many of your
problems.

Also, note that you can #include more than one .h file in a .cpp
file. You can and should do this in your .cpp files, as necessary.
The next problem (once the cyclic dependancies are fixed) is linker
errors:
eg. level.cpp refrences dsply.clean(), but the object dsply is created
by main.cpp.

Probably the easiest thing to do here (I only skimmed your design, so
this may not be the cleanest) is to pass dsply as a parameter to
level::display. In other words, make it:
void level::display(screen_display& dsply);

Good luck.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top