How do you pass a structure array to another class? For example, I want to pass the structure array point[150] to a function in another class/source file.

#include...

struct Points {
int xCoordinate;
int yCoordinate;
int zCoordinate;
} point[150];

int main(int argc, char **argv)
{ ...
void DisplayPoints();

The function I want the array to be used in:

void DisplayPoints(Point point[8].xCoordinate){ // I think this is wrong
sprintf(point[8].xCoordinate,"%.0f", 1.0 * xCoordinateText[5]);

BasicFunctionsCode.render2DBitmapText(1080, 100, (void *)font, xCoordinateText[5]);
} // end of DisplayPoints

Recommended Answers

All 6 Replies

>How do you pass a structure array to another class?
Same way you pass arrays of integers:

using std::cout;
struct Points {
int xCoordinate;
int yCoordinate;
int zCoordinate;
} point[150];
void display_points(point arr[], int size)
{
//displays all the points
  for(int i=0; i<size;++i)  
      cout<<point_arr[i].xCoordinate<<","<<
              <<point_arr[i].yCoordinate<<","<<
              <<point_arr[i].zCoordinate<<"\n";
}

I assume you want to pass the entire array to that function, then don't specify the array size. I'd also pass the element number that you want used in the sprintf() call.

void DisplayPoints(Point point[], int index){ 
    sprintf(xCoordinateText,"%.0f", point[index].xCoordinate);
    BasicFunctionsCode.render2DBitmapText(1080, 100, font, xCoordinateText);
} // end of DisplayPoints

You could also use an std::vector of your struct to avoid almost every problem that will come up.

Dave

To be specific, the problem I'm having is that the 2nd class/program file is not recognizing the structure "Points" as a type and I get the errors:

error C2061: syntax error : identifier 'Points' // from the parameters lines in the header and source files

error C2065: 'point' : undeclared identifier // from where I try to use the array

In the main file is:

struct Points {
int xCoordinate;
int yCoordinate;
int zCoordinate;
} point[150];

In the header file is:

void DisplayPoints(Points point[], int currentPoint);

In the source file is:

void DisplayPoints(Points point[], int currentPoint){ 
sprintf(point[currentPoint].xCoordinate,"%.0f", 1.0 * xCoordinateText[5]);

BasicFunctionsCode.render2DBitmapText(1080, 100, font, xCoordinateText[5]);
} // end of DisplayPoints

Place your struct type definition in .h file:

struct Points {
  int xCoordinate;
  int yCoordinate;
  int zCoordinate;
};
... global functions prototypes

Include this .h file in all modules (.cpp files) where Points type needed (in the main file too). Change array point declaration to

// main cpp
#include "h-file-name"
...
Points point[150];
...

That's all.
Think: now you have Points type definition in the main file only. Of course, it's unknow type in other files.

Thank you all, this solved the problem I was having.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.