accessing classes from a diffrent file

Hy,
I'm having a bit of trouble with classes in c++. I think I know how to define them, but here's my problem:

I have declared a class Level in the file Level.h
I have defined it in Level.cpp

I've compiled these two files, and no problem.

Now I have created a class named Block, same thing: declared in Block.h and defined in block.cpp

At last I have a file named global.h which is

1
2
3
#pragma once
#include "Level.h"
#include "Block.h 


This file is both included in Level.h and Block.h

Now here comes the problem:

In the block class I have a member of the type Level. I thought that I could just use this like:

1
2
3
4
5
6
7
8
class Block {
	private:
		...
		Level *level;
		...
	public:
		...
}


But this gives me an error and I'm pretty sure it is because it doesn't recognize the type.
So how do I make sure that the file Block.cpp knows the class Level? (I also need the reverse: members of type Block in the class Level)

Thanks for the help,

genzm

P.S.: I suppose I need to give some more code, but I will post it in the morning
If you need Level in Block and Block in Level, don't include either int either of their respective headers. Instead, forward declare the classes. Then only in the .cpp files for both classes do you include the headers.

Block.h
1
2
3
4
5
6
class Level;

class Block
{
     Level * pLevel;
};


Block.cpp
1
2
3
4
5
6
#include "Block.h"
#include "Level.h"

void Block::DoSomething(){
   pLevel->DoingStuff();
}


Level.h
1
2
3
4
5
6
class Block;

class Level
{
    Block * pBlock;
};


Level.cpp
1
2
3
4
5
6
#include "Level.h"
#include "Block.h"

void Level::DoSomethingWithBlock(){
   pBlock->Action();
}
Wow, this really did it :p
Thanks for the tip.
Topic archived. No new replies allowed.