Archived

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

MARS_999

Classes and using a struct as a member?

Recommended Posts

MARS_999    1627
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!!

Share this post


Link to post
Share on other sites
gtdelarosa    128
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;

Share this post


Link to post
Share on other sites
MARS_999    1627
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!!

Share this post


Link to post
Share on other sites
NextS    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest 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

Share this post


Link to post
Share on other sites
Promit    13246
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.

Share this post


Link to post
Share on other sites
MARS_999    1627
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!!

Share this post


Link to post
Share on other sites
barazor    122
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
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.


Share this post


Link to post
Share on other sites
MARS_999    1627
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 file

Sprite::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!!

Share this post


Link to post
Share on other sites
SabreMan    504
quote:
Original post by MARS_999
is this correct seems to be working?

Looks OK, although it is of dubious design merit. Is it really necessary to have TGAFILE as a nested class?

quote:

Also I changed my header names to
#ifndef _TILES_H_
#define _TILES_H_

No, that''s wrong. The pattern underscore-capital-anytoken is reserved by the Standard. There are other such rules. If you are not aware of them, then never use leading undescores or double underscores anywhere in your code and you will be OK.

Share this post


Link to post
Share on other sites
MARS_999    1627
quote:
Original post by SabreMan
[quote]Original post by MARS_999
is this correct seems to be working?

Looks OK, although it is of dubious design merit. Is it really necessary to have TGAFILE as a nested class?

quote:

Also I changed my header names to
#ifndef _TILES_H_
#define _TILES_H_

No, that''s wrong. The pattern underscore-capital-anytoken is reserved by the Standard. There are other such rules. If you are not aware of them, then never use leading undescores or double underscores anywhere in your code and you will be OK.

Thanks now I know about the underscore situation. On to the class nested struct for just a TGAFILE. I have a Sprite class that loads its own textures and updates everything so its just a slice of code from my class not all of it. Thanks

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

Share this post


Link to post
Share on other sites
NextS    122
Hi there,
I was wondering if anybody read my post

Well anyway. I agree with SabreMan in not using a nested class here. And I would do the initialisation of the members of TGAFILE in it''s constructor, but that''s just my 2 cents

NextS

Share this post


Link to post
Share on other sites
MARS_999    1627
quote:
Original post by NextS
Hi there,
I was wondering if anybody read my post

Well anyway. I agree with SabreMan in not using a nested class here. And I would do the initialisation of the members of TGAFILE in it''s constructor, but that''s just my 2 cents

NextS


Oops sorry!! I just didnt'' understand the syntax you gave for the tga_file = new TGAFILE(); TGAFILE is a struct. So I used in my constructor tga_file = new TGAFILE; and that worked. Now I have a different problem with wanting to use a whole different class as an argument to a function of a different class. Each class is in it''s own .cpp and .h file. So I am having trouble getting that to work now. I am confused by what you mean by nested class? You mean don''t use a struct in a class? I need a struct for a TGAFILE to be able to load it. Or do you mean use the struct inside of the LOADTGA()? Problem with that is setting up texture parameters for each texture. Thanks all for helping and if you could answer my last few questions I would appreciate it. =)



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

Share this post


Link to post
Share on other sites