Fixing circular dependencies with pointers

I've been making a project that requires different files to have access to objects declared in other files such that circular dependencies are created. I've done some research and discovered that pointers and forward declarations should be able to fix this, but I'm a little hazy on how this is done.

Example:

File 1 declares variable x, must edit x and y

File 2 must edit x and y, declares variable y

I know this isn't the best example, as you could probably declare x and y in the same file, but please suffice it to say that I'm unable to do that in my project. Thanks in advance for any help, and I'll be happy to provide code snippets if necessary.
If this variables have static storage duration then you can place their declarations in a header as for example

extern int x;
extern int y;

and include this header in the both modules. In each module there shall be a corresponding definition of an appropriate variable.
Last edited on
1
2
3
//1.cpp
extern int y;
int x;


1
2
3
//2.cpp
extern int x;
int y;


Though it might be nicer to have
1
2
3
4
//header.h
struct foo{
   static int x, y;
};


1
2
//1.cpp or 2.cpp
int foo:x, foo::y;

and use them freely in anything that includes header.h

In general you should avoid globals, unless they really make sense.
Last edited on
Thanks, but for your first example of externs, would 1.cpp and 2.cpp have to include each other? If not, how is it that the files can see the variables? Also, the variables I'm working with are objects, which I should've mentioned earlier, so the file that creates the objects needs access to the class.
Last edited on
No, cpp files should never include each other. They only need to include the header. The files can see the variables because their declarations are visible - same as for extern. The linker will later figure out what object goes to what file.
Nothing changes if you use types of your own. C++ tries hard to treat all types equally, whether they are int or some struct Foobar.
Now that I've finally had time to implement these suggestions, I'm getting a missing type specifier error in my ObjectList.h:

1
2
3
4
5
6
7
8
#ifndef ObjectList_h
#define ObjectList_h

Console console;
Editor editor;
Lists lists;
Player player;
#endif 


The types referenced are, of course, validly defined in other headers and .cpp files. Additionally, I'm getting a lot of these errors repeated, as if the include guards aren't working. Any advice?
Bump. This is a pressing issue for me, and I'd be happy to add any information someone deems necessary.
> The types referenced are, of course, validly defined in other headers
Include those headers if you are planing to use them.
Also... don't define variables in header files. Such as:

1
2
3
4
Console console;
Editor editor;
Lists lists;
Player player;


More appropriate would be:

1
2
3
4
extern Console console;
extern Editor editor;
extern Lists lists;
extern Player player;


with the actual definitions in a cpp file somewhere. Perhaps ObjectList.cpp.
Last edited on
My latest attempt:
ObjectList.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef ObjectList_cpp
#define ObjectList_cpp

#include"Player.h"
#include"Console.h"
#include"Editor.h"
#include"Lists.h"

Player player;
Console console;
Editor editor;
Lists lists;

#endif 


Player.h:
1
2
3
4
5
#include "ObjectList.cpp"
class Player
{
     // . . . .
};


Player.cpp:
1
2
#include"Player.h"
//Blah blah blah, functions try to use the "player" object but can't. 


I get errors such as:
error C2146: syntax error : missing ';' before identifier 'player'
error C2228: left of '.<name of function belonging to player>' must have class/struct/union


There are identical issues in the other headers referenced by ObjectList.cpp
You should not include *.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//object_list.h
#pragma once

#include "player.h"
extern Player player; //as we want to declare an instance, we need to include its class definition

//player.h
#pragma once
class Player{
   //...
};

//main.cpp
#include "object_list.h" //if you intend to use the `player' object

//somewhere_over_the_rainbow.cpp
#include "player.h"
Player player; //instantiating the player 


For the record, you can have several instances (read objects) of the same class.
http://upload.wikimedia.org/wikipedia/en/thumb/b/b9/MagrittePipe.jpg/300px-MagrittePipe.jpg
Last edited on
Topic archived. No new replies allowed.