Classes and using a struct as a member?

Started by
12 comments, last by MARS_999 22 years, 1 month ago
Ok I am having a feeling my struct isn''t showing up and when I go to read in data to my struct it causes my code to crash. Not sure on the correct syntax. Any help would be great. Thanks
  
#ifndef _Tiles_
#define _Tiles_

class Tiles
{
public:
    enum {MAP_SIZEY = 36};
    enum {MAP_SIZEX = 8};
    unsigned char map[MAP_SIZEY][MAP_SIZEX];    
    short tile;
    short done;
    short distance;
    float shift_x;
    float shift_y;
    float time;
    short x;
    short y;
    short z;
    short gen_number_textures;
    short texture_x;
    short texture_y;
    short texture_index;
    unsigned int texture[1];
	struct TGAFILE
	{
		unsigned char imageTypeCode;
		short int     imageWidth;
		short int     imageHeight;
		unsigned char bitCount;
		unsigned char *imageData;
	}*tga_file;

    Tiles();
    ~Tiles();
    void Init(short d, short num_textures, short tx, short ty);
    void Draw(void);
    void Time(Uint32 a, Uint32 b);
	void Update(void);
	void GenTextures(const char *path);
	bool LoadTGAFile(const char *path, TGAFILE *tga_file);
};

#endif 
  
my .cpp file where i got my LoadTGAFile() is at crashes when I go to read in the data to my tga_file struct pointer. I think its because I don''t have the data allocated before I go to load the info? Not sure if you can even put structs into a class? Thanks again Bill Gates is my Pool Boy!!
Advertisement
I think you are not allocating memory for the imageData pointer before referencing it.
You need to do something like this:

tga_file->imageData = new unsigned char[imageWidth*imageHeight];

Make sure to deallocate when it is no longer needed, like this:

delete [] tga_file->imageData;
That might be a problem but the very first variable in my struct that is used it the imageTypeCode and it crashes after that. To me that says I don''t have any memory set aside for my struct but would think it has since in main() globally I am using

  Tiles *background = new Tiles();  

and that should set aside memory for the struct but I am probably using the syntax wrong or missing something? How would I use new in my class for that struct? is the memory set aside in the constructor or in my header file?

Bill Gates is my Pool Boy!!
Hi there.

Class members are not initialized automatically. You have to do that in the constructor ( if that''s not done automatically ). If a default constructor exists for a member varible it is called. No constructors exist for basic variable types and pointers.(varies from compiler to compiler )

Since the TGAFILE *tga_file is a pointer, it points to an undefined memory region even when the constructor of Tiles gets called. So at this pointer is undefined. You could use:

struct TGAFILE {
...
}tga_file;

to automatically create an object of that type.

A different approach is to allocate memory in the constructor of the Tiles class. ( via tga_file = new TGAFILE() ).

Hope this helps
I can''t remember the exact rule, but I know for sure that the C++ standard clearly states that identifiers that begin with an underscore followed by a capital letter are reserved for compiler implementors (i.e. most people shouldn''t be using them). Change your first few lines to be more compiliant:


  #ifndef TILES_HEADER_INCLUDE_GUARD#define TILES_HEADER_INCLUDE_GUARD  
I''ve seen
#ifndef _TILES_H_
#define _TILES_H_
style header tops frequently enough. In this case, it doesn''t matter where you put the underscores as long as you stay reasonably consistent. Of course, naming a normal variable with _ or __ at the beginning would be bad.

-----------------------------
Direct3D vs. OpenGL
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.
Democracy is where you say what you want and do what you''re told.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
quote:Original post by Promit
I''ve seen
#ifndef _TILES_H_
#define _TILES_H_
style header tops frequently enough. In this case, it doesn''t matter where you put the underscores as long as you stay reasonably consistent. Of course, naming a normal variable with _ or __ at the beginning would be bad.

-----------------------------
Direct3D vs. OpenGL
The sad thing about artificial intelligence is that it lacks artifice and therefore intelligence.
Democracy is where you say what you want and do what you''re told.


Love the quote!! But my question is does it make a difference how I define my header file? e.g. your _TILES_H_ or _Tiles_
Also I tried using in my constructor tga_file = new TGAFILE; no luck I know my code works because before I tried putting this struct in my class or even using a class it worked fine in a single file with no classes and the struct was a global variable. Thanks

Bill Gates is my Pool Boy!!
Nothing is to good for me!!
quote:Original post by Anonymous Poster
I can''t remember the exact rule, but I know for sure that the C++ standard clearly states that identifiers that begin with an underscore followed by a capital letter are reserved for compiler implementors (i.e. most people shouldn''t be using them). Change your first few lines to be more compiliant:


    #ifndef TILES_HEADER_INCLUDE_GUARD#define TILES_HEADER_INCLUDE_GUARD    


actually, i believe that it was using double underscores was reserved for the compiler
quote:Original post by barazor
actually, i believe that it was using double underscores was reserved for the compiler


quote:http://www.mozilla.org/hacking/portable-cpp.html#no_reserved_names
Don''t use reserved words as identifiers.
According to the C++ Standard, 17.4.3.1.2 Global Names [lib.global.names], paragraph 1:

Certain sets of names and function signatures are always reserved to the implementation:

Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implemenation for any use.
Each name that begins with an underscore is reserved to the implementaiton for use as a name in the global namespace.

Ok, I think I got it to work. Finally. =) I am using in my class declaration this

  class Sprite{public:struct TGAFILE{//stuff}*tga_file;};//.cpp fileSprite::Sprite(){tga_file = new TGAFILE;tga_file->somevariable = 0;tga_file->somepointer = new sometypeofvaribale;}Sprite::~Sprite(){delete tga_file->somepointer;delete tga_file;}  


is this correct seems to be working? Also I changed my header names to
#ifndef _TILES_H_
#define _TILES_H_
Thanks


Bill Gates is my Pool Boy!!
Nothing is to good for me!!

This topic is closed to new replies.

Advertisement