Connecting Tech Pros Worldwide Help | Site Map

Cross-referencing in Headers

 
LinkBack Thread Tools Search this Thread
  #1  
Old June 17th, 2007, 07:25 AM
ADT_CLONE
Guest
 
Posts: n/a
Default Cross-referencing in Headers

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.


  #2  
Old June 17th, 2007, 08:05 AM
John Harrison
Guest
 
Posts: n/a
Default 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
  #3  
Old June 17th, 2007, 08:55 AM
ADT_CLONE
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.