Advertisement

good 'ol pointer problem

Started by June 21, 2001 08:49 PM
4 comments, last by Nibbles 23 years, 7 months ago
Hi, In the following code, whenever Add_House() is called, i get erros on the two lines p_house[nhouse].x ... and .y ... When the program is run, I get memory errors, and when i debug, it says:
  
x	CXX0030: Error: expression cannot be evaluated
y	CXX0030: Error: expression cannot be evaluated
z	CXX0030: Error: expression cannot be evaluated
w	CXX0030: Error: expression cannot be evaluated
h	CXX0030: Error: expression cannot be evaluated
d	CXX0030: Error: expression cannot be evaluated
  
  
typedef struct {
	GLfloat x, y, z;
	GLfloat w, h, d;
	GLuint texID[3];
} HOUSE;

HOUSE *p_house;

void Add_House() {
	nhouse++;
	p_house[nhouse].x = (float)eye.x;
	p_house[nhouse].y = (float)eye.y;
}
  
What am I doing wrong? Thanks, Scott Email Website
"If you try and don''t succeed, destroy all evidence that you tried."
it's a two part problem.

part one, is that you have a pointer to an object, not an object. try
     HOUSE *p_house = new HOUSE;     

and you'll need to delete p_House or you'll have a mem leak.


part two is now that your pointing to an object, you need to use -> instead of . to get to the sub vars


-------edit-------
and also note that the way your useing it you wight want to use new HOUSE[x] for how many you need, and then you'll need to delete p_House[];



Edited by - Authustian on June 21, 2001 10:08:59 PM
-------------------------------------------------Don't take life too seriously, you''ll never get out of it alive. -Bugs Bunny
Advertisement
basically, what i want is to have an array, but one that isn''t initially set to how big it is.

am i taking the wrong approach?

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
Here''s what you want:

to make an array of HOUSE''s of size nhouse, you use:

  p_house = new HOUSE[nhouse];  


To increase the size of this array, you need to make a new array, and copy all the elements over. e.g.:

  void increase_array(){    HOUSE *temp = new HOUSE[nhouse + 1];    memcpy( temp, house, nhouse * sizeof(HOUSE) );    delete[] p_house;    p_house = temp;    nhouse++;}  


Also, to access the last element in array, use this:

  p_house[nhouse - 1].x = whatever...  


Finally, when you''re finished with the array, you have to delete it with:

  delete[] p_house;  


Hope this helps!


War Worlds - A 3D Real-Time Strategy game in development.
Ooooooohhhh... So that''s how you do all of that creating, and destroying stuff

(can''t wait to get home... stupid work).

Thanks,
Scott

Email
Website

"If you try and don''t succeed, destroy all evidence that you tried."
If you REALLY want to get into all "this creating and destroying stuff" you might wanna have a peek at linked lists, theres a good tutorial on this website. It takes a while to understand fully at first, but a knowledge of pointers and basic memory allocation is all you need to start using them properly.
--------------------Go Stick Your Head In A Pig

This topic is closed to new replies.

Advertisement