Hello there,
I'm very new to C++ and I'm mainly using it for graphics using the opengl libraries.
Anyway, I'm having trouble with some initialisation. I am writing a file reader class that will read a file containing vertex and face data for a series of polygons.
Data.h
#ifndef _DATA_H_
#define _DATA_H_
// Vertex class holds x, y, and z values, and provides the getter
// and setter methods.
class Vertex {
public:
Vertex() { x = y = z = 0.0; }
~Vertex() { }
float getX();
float getY();
float getZ();
void setX(float);
void setY(float);
void setZ(float);
private:
float x, y, z;
};
// Face class contains an array of ints that will be used to reference
// the vertices when creating a shape.
class Face {
public:
Face();
Face(int);
~Face() { }
int* getPoints();
int getNumberOfPoints();
void addPoint(int, int);
private:
int numberOfPoints;
int* points;
};
#endif
Data.cpp
/***************************************************************
* The Data.cpp file provides the functions definitions for *
* the classes defined in Data.h. These will be used when *
* the data file is to be read in and stored. *
***************************************************************/
#include "Data.h"
// Vertex getters
float Vertex::getX() { return x; }
float Vertex::getY() { return y; }
float Vertex::getZ() { return z; }
// Vertex setters
void Vertex::setX(float newX) { x = newX; }
void Vertex::setY(float newY) { y = newY; }
void Vertex::setZ(float newZ) { z = newZ; }
// Face constructor
Face::Face()
{
numberOfPoints = 0;
points = new int[10];
}
Face::Face(int no)
{
numberOfPoints = no;
points = new int[no];
}
void Face::addPoint(int location, int p)
{
*(points + location) = p;
}
// Getter for the array
int* Face::getPoints()
{
return points;
}
int Face::getNumberOfPoints()
{
return numberOfPoints;
}
DataReader.h
#ifndef _DATAREADER_H_
#define _DATAREADER_H_
#include "Data.h"
class Reader {
public:
Reader(char[]);
~Reader() {}
void readFile();
Vertex getVertices();
Face getFaces();
private:
char *file;
Vertex* verticies;
Face* faces;
};
#endif
DataReader.cpp
/***************************************************************
* The FileReader class reads external data files, and creates *
* arrays to store the read data in. The data to be read is a *
* range of verticies, and data about faces. *
***************************************************************/
#include <iostream>
#include <fstream>
#include "Data.h"
#include "DataReader.h"
using namespace std;
Reader::Reader(char c[])
{
file = c;
}
void Reader::readFile()
{
//set up file stream
ifstream file(file);
if (file.is_open())
{
//read in the number of verticies
int numberOfVerticies;
file >> numberOfVerticies;
cout << "VERTICIES\nNumber of Verticies: " << numberOfVerticies << "\n";
//set the array to hold the number of vertex objects needed
verticies = new Vertex[numberOfVerticies];
//for each vertex
for (int i = 1; i <= numberOfVerticies; i++)
{
float vNumber, x, y, z;
Vertex v;
//read in values and store them in the vertex object
file >> vNumber >> x >> y >> z;
v.setX(x);
v.setY(y);
v.setZ(z);
//add the vertex to the array
*(verticies + i) = v;
cout << "Vertex " << i << " - X: " << v.getX() << " Y: " << v.getY() << " Z: " << v.getZ() << "\n";
}
cout << "\n";
//check that the array has the data stored correctly
for (int p = 1; p <= numberOfVerticies; p++)
{
Vertex v = *(verticies + p);
cout << "Vertex " << p << " - X: " << v.getX() << " Y: " << v.getY() << " Z: " << v.getZ() << "\n";
}
cout << "\nFACES\n";
//read in the number of faces
int numberOfFaces;
file >> numberOfFaces;
cout << "Number of Faces: " << numberOfFaces << "\n";
//initialise the array to the size of the number of faces
faces = new Face[numberOfFaces];
//for each face
for (int j = 0; j < numberOfFaces; j++)
{
//find the number of points for each face
int numberOfPoints;
file >> numberOfPoints;
cout << "\nFace " << j << "\nNumber of Points: " << numberOfPoints << "\n";
//create a new Face, initialising the int array size to the number of points
Face f(numberOfPoints);
//for each point
for (int k = 0; k < numberOfPoints; k++)
{
//read the point and store it in the array
int point;
file >> point;
f.addPoint(k, point);
cout << point;
}
//add the face to the array of faces
*(faces + j) = f;
}
}
file.close();
}
I'm sorry for all the code, I'm just stuck for options, and have tried a lot of different ways to get around it.
Objects of class reader each have an array of verticies and faces. These are pointers, that are initialised in the readFile method. I want to initialise each pointer to an array, where the size is read in from the input file. verticies is initialised to an array of size numberOfVerticies, using the default constructor.
What I need for faces, is an array of face objects. Each face object has an int pointer, to an array on ints, the size being the number of points read in.
The problem when I run it, the cmd window appears, but then I get an illegal exception error and the window crashes. It reads the vertices correctly, but crashes when reading the faces. What is strange is that when I step through with the debugger it works fine, yet when I run it , it crashes.
I know I'm being vague, so please ask if you want me to clear anything up.
Thanks very much, I know there is a lot of code there to sift through.
ps I know i've spelt vertices as verticies in some of the bits of code, I've sorted that typo in the real thing :D