• Advertisement

Archived

This topic is now archived and is closed to further replies.

help with problem please

This topic is 5811 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, sorry, I didn't exactly know what to call this cause it isn't an I/O or a model problem. Since I was looking at a post talking about models, I decided to make my own fun little model format. I made a console program to check and see if my model format was working, but I'm running into a few problems. They have to do with arrays (I want to make an array of vertexes, except the number of vertexes in the array is extracted from the model format, so the compiler thinks it's 0 or something). Here's the code: // model1.cpp : Sample program for testing model format // #include "stdafx.h" struct vertex // structure to hold vertex data { float x; // x float y; // y float z; // z }; int main(int argc, char* argv[]) { char name[80]; // name of model int numVertices; // number of vertices // create and open model file ifstream model("model1.txt"); // check if model loading worked if (!model) { cout << "Cannot open model1.txt"; return 1; } // display model's name cout << "Model Name: "; model >> name; // extract model's name cout << name << endl; // print model's name cout << endl; // skip a space model >> numVertices; // extract number of vertices // print number of vertices cout << "Number of vertices: " << numVertices << endl; // skip a space cout << endl; // create appropriate amount of vertices (((ERROR HERE))) vertex vertice[numVertices]; // print vertices for (int i=0; i> vertice.x; cout << vertice[i].x << endl; // ,y cout << "Y: "; model >> vertice[i].y; cout << vertice[i].y << endl; // .z cout << "Z: "; model >> vertice[i].z; cout << vertice[i].z << endl; cout << endl; // skip a space } return 0; // end program } The problem is vertex vertice[numVertices]; I'll go ahead and show you my model thingy too: Model1 3 0.0 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 Sorry, I don't exactly know the differences between vertexes and vertices, so if it looks messed up, ya. If you could tell me the difference, that would be great. But note that the 3 is the number of vertices. The compiler errors are: model1.cpp(44) : error C2057: expected constant expression model1.cpp(44) : error C2466: cannot allocate an array of constant size 0 model1.cpp(44) : error C2133: 'vertice' : unknown size (Line 44 is the vertex vertice[numVertices]; line) If anyone could help me with this, thanks in advance. Ex.. ex.. excuse me.. excuse me... I didn't... I didn't get a piece of... I didn't get a piece of cake I didn't get a piece of cake Edited by - tuxx on February 23, 2002 4:26:43 PM

Share this post


Link to post
Share on other sites
Advertisement
you''ll have to dynamically allocate memory for the number of vertices..

vertex *vertice = new vertex[numVertices];
..
..
..
delete [] vertice;

that should fix that

Share this post


Link to post
Share on other sites
You can''t define a static array with a variable. You have to make it dynamic like this:

vertex *vertices = new vertex[numVertices];

then you just use it normally except before your program terminates call

delete [] vertices;

or you get memory leaks.

PaladinGLT

Share this post


Link to post
Share on other sites
Woohoo! It works! Thanks. I never thought I''d get a reply 20 minutes after I posted that totally fixed the problem. Maybe I should have put this in the Beginners section... But anyways, thanks!

Ex.. ex.. excuse me.. excuse me... I didn''t... I didn''t get a piece of... I didn''t get a piece of cake I didn''t get a piece of cake

Share this post


Link to post
Share on other sites
The problem is that you have to supply a constant value when defining an array, but numVertices is a variable.
Since you need an array with dynamic size, you''ll have to change your code so that you have a pointer, allocate memory for your vertices, let the pointer point to this memory and when you''re finished you''ll have to free the memory.
  
// Somewhere at the top or wherever you like it

vertex *vertices;

// Just before you load your vertex data

vertices = (vertex*)malloc(sizeof(vertex)*numVertices); // Allocate memory for the vertices and let the pointer point at this memory


// When you''re finished with your vertex data

free(vertices); // Free the allocated memory



This creates and destroys an array with enough memory to hold exactly numVertices vertices. If you didn''t know, you can access this array exactly like a constant array using vertices[index].

Vertexes? Never heard that word. The correct plural of vertex is vertices. I guess you meant vectors. The difference between vertices and vectors is that when you talk of vertices you simply mean points in space and vectors are more like arrows. For example the velocity of a moving object can be expressed as a vector. The length of the vector/arrow is the speed and the direction of the vector/arrow is the direction of the moving object.

I hope you understand what I mean and it helps you.

Share this post


Link to post
Share on other sites

  • Advertisement