Nov 21, 2009

C++ Struct?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General

C++ Struct?

sparkx
I am real new to c++ and really don't know much about it at all. I (always jumping right into a language) am having a problem while making a 3D loader. I have no clue how this works but I am basing my VRML loader off of a MS3D one I found. I can "dissect" the file just fine except for one thing. My struct seems to change every time I call it. Here is some code I’m not sure if you need more:
CODE
struct Material
{
  float m_ambient[8];
  float m_diffuse[8];
};

[...More code...]

Material *def_ambientIntensity = new Material[1];
memcpy( def_ambientIntensity[1].m_ambient, ambientIntensity->amb, 17);
memcpy( def_ambientIntensity[1].m_diffuse , ambientIntensity->dif, 26);

//Next my noob way to see what the variable is.
MessageBox( NULL, (char*)def_ambientIntensity[1].m_diffuse, "Diffuse", MB_OK | MB_ICONERROR ); //Good
MessageBox( NULL, (char*)def_ambientIntensity[1].m_diffuse, "Diffuse", MB_OK | MB_ICONERROR ); //Bad?
Anyone see a problem? I have no clue why the value of "def_ambientIntensity[1].m_diffuse" would change. Please keep your explanation as simple as possible I just started learning c++ about 4 days ago...

Thanks,
Sparkx

 

 

 


Comment/Reply (w/o sign-up)

mitchellmckain
QUOTE(sparkx @ Oct 24 2008, 11:24 AM) *
I am real new to c++ and really don't know much about it at all. I (always jumping right into a language) am having a problem while making a 3D loader. I have no clue how this works but I am basing my VRML loader off of a MS3D one I found. I can "dissect" the file just fine except for one thing. My struct seems to change every time I call it. Here is some code I’m not sure if you need more:
CODE
struct Material
{
  float m_ambient[8];
  float m_diffuse[8];
};

[...More code...]

Material *def_ambientIntensity = new Material[1];
memcpy( def_ambientIntensity[1].m_ambient, ambientIntensity->amb, 17);
memcpy( def_ambientIntensity[1].m_diffuse , ambientIntensity->dif, 26);

//Next my noob way to see what the variable is.
MessageBox( NULL, (char*)def_ambientIntensity[1].m_diffuse, "Diffuse", MB_OK | MB_ICONERROR ); //Good
MessageBox( NULL, (char*)def_ambientIntensity[1].m_diffuse, "Diffuse", MB_OK | MB_ICONERROR ); //Bad?
Anyone see a problem? I have no clue why the value of "def_ambientIntensity[1].m_diffuse" would change. Please keep your explanation as simple as possible I just started learning c++ about 4 days ago...

Thanks,
Sparkx


Well the array indices in c++ usually start at 0 not at 1, so it looks to me like you are referencing a nonexistent member of the array. This is one of the biggest troubles with c++ that you have to do all your own checking to make sure you don't reference nonexistent members of an array, for doing so will trigger no errors, you just get unpredicable results.

I think that is it, but if not then it would nice to see what the output looks like, but my guess would be that you have found a bug in your compiler, because they did not quite anticipate how you are using this feature. In that case, what happens without using an array at all or a longer array?

 

 

 


Comment/Reply (w/o sign-up)

sparkx
I think I solved that problem. Thanks for your help. All I did was change \"... ambientIntensity->amb, 17\" to \"ambientIntensity->amb, 8*1\" and it seemed to work but now I have a new problem. This is a little frustrating, seems like one thing after another. Now I have a \"GLfloat *\" and I need to convert it to a \"GLfloat\" but nothing I try works. I can\'t just do \"(GLfloat&) ...\" because it doesn\'t convert correctly or something (the color stays at 0.0 when GLfloat * is 1.0). I am trying to put in the colors with glColor3f(R, G, B); but it seems that I can\'t just put \"glColor3f((float) def_color->m_colorR ..)\". I get \"pointer value used where a floating point value was expected\". I read somewhere that I could fix this with an & but this doesn’t work either \"...&def_color->...\". What do I need to do to convert my \"pointer value\" to a \"floating point value\"? I have tried memcpy() and that won\'t convert it correctly and sometime it make my program un-responsive.

Any Suggestions?
Thanks,
Sparkx

Edit: Smiles were on...

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Struct


    Looking for c, struct,

See Also,

*SIMILAR VIDEOS*
Searching Video's for c, struct,
advertisement



C++ Struct?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com