Hi,
I am still rather green when it comes to C++ so be gentle with me. I am trying to create a dynamic array of objects (class Structure), each object represents a geometric (physical) structure. But for the sake of simplicity let's consider only one such object. Within each instance of Structure there are a number of planes, let's say i, each plane i has a variable number of points, j. Each point consists of three floats representing (x,y,z) coordinate triplets. This is standardised format of the data I have to read in.
I have created a simple second class, Point, which is supposed to contain one coordinate (x,y,z)triplet representing [i,j]. I therefore need to create a dynamic array of Point objects of size i,j(i) within the Structure object. I hope this is clear.
Clearly I am making some fundamental since the code will not compile.
I would appreciate some assistance with this.
Thanks
// Define Point class to assign (x,y,z) values
// Create a 2D array of Point objects for each Structure
// i.e. Point[i][j] where
// i = number of planes in Structure (numPlanes)
// j = number of points in plane_i of Structure (numPoints(i))
// Last modified 23.03.09
#include<iostream>
#include<ctype.h>
#include<math.h>
using namespace std;
class Point{
public:
float x,y,z;
// Point methods to set
void SetX (float x) {x=x;}
void SetY (float y) {y=y;}
void SetZ (float z) {z=z;}
// constructor and destructor
Point(){
x=y=z=0.0;
};
Point::~Point();// destructor
};
// class Structure into which points are read in
class Structure{
public:
int referenceNumber,numPlanes,numP;
int *numPoints;
// int numPoints[];
char referenceName[30], fileName[80];
Structure(); // parameterless constructor
// Structure(char fileName);
~Structure(); // destructor
};
Structure::Structure(){
// Initialise everything to zero
int i,j,k; // i is number of planes in (structure)
// j is number of points contained in (plane)_i - this is variable within
// (structure) => consider use of vector
// k is index to step through coordinates of point i.e. x(k=0),y(k=1),z(k=2)
referenceNumber=numPlanes=numP=0;
// Get reference number of structure_i - not needed for this simple test
referenceNumber = 0;
// Get reference name of structure_i - not needed for this simple test
// Get number of planes (j) contained in structure_i
cout << "Enter number of planes : \n";
cin >> numPlanes;
int *numPoints = new int[numPlanes]; // declare dynamic integer array for points[j] in each plane[i]
Point **pPoint = new Point*[numPlanes]; // declare dynamic **pPoints object array[i]
for (i=0;i<numPlanes;i++) {
//Initialise numPoints[i] and pPoint[i] to zero
numPoints[i]=0;
// pPoint[i]=0;
}
// Get number of points (j) contained in plane i of structure
for (i=0;i<numPlanes;i++){
cout << "Enter number of points in plane " << i+1 << " : \n";
cin >> numPoints[i];
}
//Allocate memory for 2D array of Point objects
for (i=0;i<numPlanes;i++) pPoint[i]=NULL;
for (i=0;i<numPlanes;i++){
numP =numPoints[i];
pPoint[i] = new Point [numP];
if (pPoint[i]==NULL){
cout << "\nNot enough memory for points.\n";
// throw error exception class
}
//Now set the values of points to zero
for (j=0;j<numP;j++){
// call default constructor to set x,y,z to zero
Point();
}
}
}
Structure::~Structure(){
int j;
cout << "\nFreeing up memory...\n";
for (j=0;j<numPoints;j++) delete [] pPoint[j];
// then the rows...
delete [] pPoint;// calls pPoint destructor and releases memory (avoid memory "leak")
if (numPoints !=NULL){
delete [] numPoints;
numPoints=NULL;
}
cout << "Done!\n";
}
int main(){
return 0;
}