By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

Cross-referencing in Headers

P: n/a
I am having trouble with headers at the moment. Here is the code for a
start:

body.h:

#pragma once

#ifndef BODY_H
#define BODY_H

class body
{
private:
joint* jointList;
};

#endif

joint.h:

#pragma once

#ifndef JOINT_H
#define JOINT_H
class joint
{
public:
int getRelativeX(){return r_X;}
int getRelativeY(){return r_Y;}
private:
//The x variable is actually relative to the master body
int r_X;//Relative X
int r_Y;//Relative Y
body* r_Body;//Relative Body
};

#endif

main.cpp:

/*--------Declarations--------*/

#include <iostream>

#include "joint.h"
#include "body.h"

using namespace std;

int main()
{
cout<<"Hello World";
cin.get();
return 1;
}

Now the problem I am encountering is that when I compile this it has
errors with the following line:

body* r_Body;//Relative Body

I know this is because somehow the headers haven't linked properly.
The problem is that joint needs body and body needs joint. So how
would I do this, because I'm stumpted. Thanks for your help in advance.

Jun 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ADT_CLONE wrote:
I am having trouble with headers at the moment. Here is the code for a
start:

body.h:

#pragma once

#ifndef BODY_H
#define BODY_H

class body
{
private:
joint* jointList;
};

#endif

joint.h:

#pragma once

#ifndef JOINT_H
#define JOINT_H
class joint
{
public:
int getRelativeX(){return r_X;}
int getRelativeY(){return r_Y;}
private:
//The x variable is actually relative to the master body
int r_X;//Relative X
int r_Y;//Relative Y
body* r_Body;//Relative Body
};

#endif

main.cpp:

/*--------Declarations--------*/

#include <iostream>

#include "joint.h"
#include "body.h"

using namespace std;

int main()
{
cout<<"Hello World";
cin.get();
return 1;
}

Now the problem I am encountering is that when I compile this it has
errors with the following line:

body* r_Body;//Relative Body

I know this is because somehow the headers haven't linked properly.
The problem is that joint needs body and body needs joint. So how
would I do this, because I'm stumpted. Thanks for your help in advance.
You need forward declarations

#ifndef BODY_H
#define BODY_H

class joint; // forward declaration

class body
{
private:
joint* jointList;
};

#endif

#ifndef JOINT_H
#define JOINT_H

class body; // forward declaration

class joint
{
public:
int getRelativeX(){return r_X;}
int getRelativeY(){return r_Y;}
private:
//The x variable is actually relative to the master body
int r_X;//Relative X
int r_Y;//Relative Y
body* r_Body;//Relative Body
};

#endif

john
Jun 17 '07 #2

P: n/a
On Jun 17, 6:00 pm, John Harrison <john_androni...@hotmail.comwrote:
ADT_CLONE wrote:
I am having trouble with headers at the moment. Here is the code for a
start:
body.h:
#pragma once
#ifndef BODY_H
#define BODY_H
class body
{
private:
joint* jointList;
};
#endif
joint.h:
#pragma once
#ifndef JOINT_H
#define JOINT_H
class joint
{
public:
int getRelativeX(){return r_X;}
int getRelativeY(){return r_Y;}
private:
//The x variable is actually relative to the master body
int r_X;//Relative X
int r_Y;//Relative Y
body* r_Body;//Relative Body
};
#endif
main.cpp:
/*--------Declarations--------*/
#include <iostream>
#include "joint.h"
#include "body.h"
using namespace std;
int main()
{
cout<<"Hello World";
cin.get();
return 1;
}
Now the problem I am encountering is that when I compile this it has
errors with the following line:
body* r_Body;//Relative Body
I know this is because somehow the headers haven't linked properly.
The problem is that joint needs body and body needs joint. So how
would I do this, because I'm stumpted. Thanks for your help in advance.

You need forward declarations

#ifndef BODY_H
#define BODY_H

class joint; // forward declaration

class body
{
private:
joint* jointList;

};

#endif

#ifndef JOINT_H
#define JOINT_H

class body; // forward declaration

class joint
{
public:
int getRelativeX(){return r_X;}
int getRelativeY(){return r_Y;}
private:
//The x variable is actually relative to the master body
int r_X;//Relative X
int r_Y;//Relative Y
body* r_Body;//Relative Body

};

#endif

john

Oh ok, thanks a lot. I thought it was something like that. I'm a bit
rusty and was trying to do it the wrong way. :D

Jun 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.