Connecting Tech Pros Worldwide Forums | Help | Site Map

Cross-referencing in Headers

ADT_CLONE
Guest
 
Posts: n/a
#1: Jun 17 '07
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.


John Harrison
Guest
 
Posts: n/a
#2: Jun 17 '07

re: Cross-referencing in Headers


ADT_CLONE wrote:
Quote:
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
ADT_CLONE
Guest
 
Posts: n/a
#3: Jun 17 '07

re: Cross-referencing in Headers


On Jun 17, 6:00 pm, John Harrison <john_androni...@hotmail.comwrote:
Quote:
ADT_CLONE wrote:
Quote:
I am having trouble with headers at the moment. Here is the code for a
start:
>
Quote:
body.h:
>
Quote:
#pragma once
>
Quote:
#ifndef BODY_H
#define BODY_H
>
Quote:
class body
{
private:
joint* jointList;
};
>
Quote:
#endif
>
Quote:
joint.h:
>
Quote:
#pragma once
>
Quote:
#ifndef JOINT_H
#define JOINT_H
>
Quote:
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
};
>
Quote:
#endif
>
Quote:
main.cpp:
>
Quote:
/*--------Declarations--------*/
>
Quote:
#include <iostream>
>
Quote:
#include "joint.h"
#include "body.h"
>
Quote:
using namespace std;
>
Quote:
int main()
{
cout<<"Hello World";
cin.get();
return 1;
}
>
Quote:
Now the problem I am encountering is that when I compile this it has
errors with the following line:
>
Quote:
body* r_Body;//Relative Body
>
Quote:
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

Closed Thread