PDA

View Full Version : custom routines and MSC++



nexusone
07-13-2000, 07:39 AM
I have been getting back into programming after of being out of it for a few year's.
I want to seperate my routines into diffrent .cpp files and use my own .h files.

I am using the code from nehe site to setup the window and opengl setup code.
The problem I am having is passing a structure from my routine.


//mystuff.h

struct XYZ_BUFFER
{
float x;
float y;
float z;
int dir_x;
int dir_y;
int dir_z;
};
// end .h


// mystuff.c

#include "mystuff.h" //defines my structure.

XYZ_BUFFER Check_boundary( XYZ_BUFFER bc )
{
// check location of object
// change direction if needed
// code not shown but works when in main program.
return( bc )
}

What is the proper way to access from another my main.c program.
It works when I have it all in one file....

MikeC
07-13-2000, 08:19 AM
What error message are you getting?

EricK
07-13-2000, 08:47 AM
Just guessing, but I noticed that you passed bc by value. You might want to pass by reference:
XYZ_BUFFER Check_boundary( XYZ_BUFFER & bc )

nexusone
07-13-2000, 10:55 AM
The error message is during linking...
LNK 2001 unresolved external symbol

here is how my routine is defined in my main.c program.

XYZ_BUFFER xyz_object[64];

XYZ_BUFFER Check_boundary(XYZ_BUFFER);

what is the correct way to call a routine
and pass a varible structure from another .c file.

phlake
07-13-2000, 12:44 PM
this is a dumb question, but you're including "mystuff.h" in main.c, right?

and mystuff.h should have a prototype for the function you're calling in mystuff.c

[This message has been edited by phlake (edited 07-13-2000).]

nexusone
07-13-2000, 01:36 PM
Originally posted by phlake:
this is a dumb question, but you're including "mystuff.h" in main.c, right?

and mystuff.h should have a prototype for the function you're calling in mystuff.c

[This message has been edited by phlake (edited 07-13-2000).]

Yea, I have the mystuff.h in both mystuff.c and main.c

nexusone
07-13-2000, 01:45 PM
If anyone is interested I can zip the code and e-mail it to you...

I know it has to be something very simple!

DFrey
07-13-2000, 01:48 PM
Is one file a .cpp and another .c? That can cause linker errors like that, if the call type isn't explicity indicated.

07-14-2000, 06:46 AM
In your .h try
typedef struct
{
...
} XYZ_BUFFER;

Alternatively, in your .cpp
change references to XYZ_BUFFER to
'struct XYZ_BUFFER'.

07-14-2000, 07:43 AM
Originally posted by Troy:
In your .h try
typedef struct
{
...
} XYZ_BUFFER;

Alternatively, in your .cpp
change references to XYZ_BUFFER to
'struct XYZ_BUFFER'.

Ooops... you can see my C background bias...
I notice that this isn't necessary for C++

So, the second part of phlake's 'dumb'
question still applies, does your
.h file include a function prototype?

XYZ_BUFFER Check_Boundary(XYZ_BUFFER bc);

nexusone
07-14-2000, 05:02 PM
Here is some more code.

//mysuff.h

typedef struct // this structure definded for the location of each pyramid
{
float x;
float y;
float z;
int dir_x;
int dir_y;
int dir_z;
}XYZ_BUFFER;

// end mystuff.h

//mystuff.cpp

#include "mystuff.h"

XYZ_BUFFER check_boundary( XYZ_BUFFER xyz_ptr )
{

if ((xyz_ptr.x > 2.0f) | (xyz_ptr.x < -2.0f))
{
xyz_ptr.dir_x = xyz_ptr.dir_x * -1;
};

if ((xyz_ptr.y > 2.0f) | (xyz_ptr.y < -2.0f))
{
xyz_ptr.dir_y = xyz_ptr.dir_y * -1;
};

if ((xyz_ptr.z > 6.0f) | (xyz_ptr.z < -6.0f))
{
xyz_ptr.dir_z = xyz_ptr.dir_z * -1;
};

return( xyz_ptr );
}

// end mystuff.cpp

// part of the code of pyramid.cpp (main)

XYZ_BUFFER xyz_buffer[64]; // this is the location varible for each pyramid

XYZ_BUFFER check_boundary( XYZ_BUFFER ); // define my routine


check_boundary(xyz_buffer[63]); // how I call my routine.

//end pyramide.cpp

what you think???

nexusone
07-16-2000, 07:03 AM
I have got the program to work now, thanks for the input.

Eric