Jump to content
xisto Community
Sign in to follow this  
sparkx

C++ Struct?

Recommended Posts

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:

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 ); //GoodMessageBox( 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

Share this post


Link to post
Share on other sites

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:

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 ); //GoodMessageBox( 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?

Share this post


Link to post
Share on other sites

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,SparkxEdit: Smiles were on...

Edited by sparkx (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.