Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-21-2007, 02:17 AM
Sheemer Sheemer is offline
Newbie
 
Join Date: Aug 2007
Posts: 1
Rep Power: 0
Sheemer is on a distinguished road
Default 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 11:19 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to binary file paul. Pascal/Delphi 4 04-13-2007 09:23 PM
Writing to a File - Shell Script Help! dirkfirst General Programming 2 10-03-2006 07:02 PM


All times are GMT -5. The time now is 02:05 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads