Cross-referencing in Headers

A

ADT_CLONE

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.
 
J

John Harrison

ADT_CLONE said:
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
 
A

ADT_CLONE

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
 

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,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top