+ Reply to Thread
Results 1 to 1 of 1

Thread: Issue writing to file: pointer to a class which contains pointers to other classes

  1. #1
    Newbie Sheemer is an unknown quantity at this point
    Join Date
    Aug 2007
    Posts
    1

    Issue writing to file: pointer to a class which contains pointers to other classes

    Hi. I'm hoping someone can help me out here. I wrote a model loader for Milkshape 3D files and it works fine and dandy. However, it is currently reading in the files as text based documents. After loading in the models I am trying to write them out to file in binary to reduce the file size of the models.

    I am trying to write out a pointer to my model class, and this MOSTLY works. Any data values I write out do indeed work properly and even load back in properly. The problem is that my model class has pointers to other classes contained within it. The class I am focusing on right now is called Mesh.

    So, when I write out Model, the data get written, but the information contained in Model->Mesh does not. What seems to happen is that it writes out the memory address of the pointer instead.

    So, I tried writing Copy Constructors and operator assignments for Model and Mesh, but neither of these have helped solve the issue.

    Below is the class definitions and Copy Constructors/Operator Assignments.

    Code:
    class Mesh
    {
    public:
    
    	// Constructor
    	Mesh();
    	// Copy Constructor
    	Mesh(const Mesh&);
    	// Operator Assignment
    	Mesh & operator= (const Mesh &M);
    	// Destructor
    	~Mesh();
    	// Mesh bones
    	void MeshBone(int bonenum);
    	// Find face neigbours for shadowing
    	void NeighbourFaces();
    	// Set triangle planes
    	void SetPlanes();
    
    	// Name Length
    	int namelength;
    	// Name
    	char *name;
    	// Flag
    	int flag;
    	// Material Index
    	int matindex;
    	
    	// Number of Vertices
    	int vertnum;
    	// Vertices
    	Vertex *verts;
    
    	// Number of Normals
    	int normnum;
    	// Normals
    	Normal *norms;
    
    	// Number of Triangles
    	int trinum;
    	// Triangles
    	Triangle *tris;
    
    	// bone
    	int bone;
    };
    
    
    
    class Model
    {
    public:
    
    	// Constructor
    	Model();
    	// Copy Constructor
    	Model(const Model&);
    	// Operator Assignment
    	Model & operator= (const Model &M);
    	// Destructor
    	~Model();
    	// Set parent bones
    	void SetPBones();
    
    	void Exit();
    	// Load Model
    	int LoadModel(char *filename);
    	// Draw Model
    	void Draw();
    	// Remove Quotes
    	void RemoveQuotes(char *array);
    	// Degree to Radian
    	float DegtoRad(float degree);
    	// Radion to Degree
    	float RadtoDeg(float radian);
    	// Triangle bones
    	void TriBones();
    
    	// Number of Meshes
    	int meshnum;
    	// Meshes
    	Mesh *mesh;
    
    	// Number of Materials
    	int matnum;
    	// Materials
    	Material *material;
    
    	// Number of Bones
    	int bonenum;
    	// Bones
    	Bone *bone;
    
    	// Total Frames
    	int tframe;
    	// Current Frame
    	int cframe;
    	
    	// Did Model Load? 1 for yes
    	int loaded;
    	// animated model
    	int animate;
    };
    
    
    
    Mesh::Mesh(const Mesh &M)
    {
    	namelength = M.namelength;
    	flag = M.flag;
    	matindex = M.matindex;
    	vertnum = M.vertnum;
    	normnum = M.normnum;
    	trinum = M.trinum;
    	bone = M.bone;
    	name = new char[namelength];
    	//strcpy(name, M.name);
    	name = M.name;
    	verts = new Vertex;
    	verts = M.verts;
    	norms = new Normal;
    	norms = M.norms;
    	tris = new Triangle;
    	tris = M.tris;
    };
    
    Mesh & Mesh::operator = (const Mesh &M)
    {
    	if (this == &M)
    		return *this;
    
    	else
    	{
    		namelength = M.namelength;
    		flag = M.flag;
    		matindex = M.matindex;
    		vertnum = M.vertnum;
    		normnum = M.normnum;
    		trinum = M.trinum;
    		bone = M.bone;
    		delete [] name;
    		name = new char[namelength];
    		//strcpy(name, M.name);
    		name = M.name;
    		delete verts;
    		verts = new Vertex;
    		verts = M.verts;
    		delete norms;
    		norms = new Normal;
    		norms = M.norms;
    		delete tris;
    		tris = new Triangle;
    		tris = M.tris;
    		return *this;
    	}
    };
    
    
    
    Model::Model(const Model &M)
    {
    	mesh = new Mesh;
    	mesh = M.mesh;
    	material = new Material;
    	material = M.material;
    	bone = new Bone;
    	bone = M.bone;
    	meshnum = M.meshnum;
    	matnum = M.matnum;
    	bonenum = M.bonenum;
    	tframe = M.tframe;
    	cframe = M.cframe;
    	loaded = M.loaded;
    	animate = M.animate;
    };
    
    Model & Model::operator = (const Model &M)
    {
    	if (this == &M)
    		return *this;
    
    	else
    	{
    		delete mesh;
    		mesh = new Mesh;
    		mesh = M.mesh;
    		delete material;
    		material = new Material;
    		material = M.material;
    		delete bone;
    		bone = new Bone;
    		bone = M.bone;
    		meshnum = M.meshnum;
    		matnum = M.matnum;
    		bonenum = M.bonenum;
    		tframe = M.tframe;
    		cframe = M.cframe;
    		loaded = M.loaded;
    		animate = M.animate;
    		return *this;
    	}
    };
    Finally, for my writing I am using the format of:

    Code:
    Model *model = new Model;
    model->LoadModel(filename);
    
    ofstream saver("model.txt", ios::out | ios::binary);
    saver.write( (char *) model, sizeof(Model));
    saver.close();
    Does anyone have any idea what I can do to solve this issue? Thanks. Also, this is my first post so I apologize if the code snipets don't come out in a box like I was hoping.

    *Addition*
    I have since made a small test program and have found that if I don't use pointers the information seems to write perfectly fine. It's only when I make pointers that the problem occurs. If possible I'd rather not modify my entire loader, so help would still be appreciated.

    *Addition*
    Now that I think about it, it is likely not possible to simply write an object that has dynamically allocated memory anyways. I guess I'll just write a function to save it out.
    Last edited by Sheemer; 08-21-2007 at 10:19 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Writing to binary file
    By paul. in forum Pascal/Delphi
    Replies: 4
    Last Post: 04-13-2007, 08:23 PM
  2. Writing to a File - Shell Script Help!
    By dirkfirst in forum General Programming
    Replies: 2
    Last Post: 10-03-2006, 06:02 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts