Sign in to follow this  
bengaltgrs

very simple

Recommended Posts

Guest Anonymous Poster
Quote:
Original post by bengaltgrs
Very simple but apparently I'm missing something.
Why would this not compile?


int offsets[2];

offsets[0] = 5;
offsets[1] = 3;

You're probably trying to do the second two lines outside of a function, which isn't valid.

int offsets[2] = { 5, 3 }; // Valid

offsets[0] = 5; // Not valid
offsets[1] = 3; // Not valid

int main()
{
offsets[0] = 5; // Valid
offsets[1] = 3; // Valid
}

Share this post


Link to post
Share on other sites
Make a new project and write out the full example. Give us the error messages you are getting.

e.g.


int main() {
int offsets[2];

offsets[0] = 5;
offsets[1] = 3;
return 0;
}



There shouldn't be any problem with the code you have shown so you are either not showing us the real code or you've made a mistake in the preceding lines, or a macro has been defined (such as #define offsets) which is messing eveything up.

Share this post


Link to post
Share on other sites
Ok, this is what I am actually trying to do:

D3DXVECTOR3 vecLocalPoints[8];

D3DXVECTOR3 vecWorldPoints[8];

float Width = 2.0f;
float Height = 2.0f;
float Depth = 2.0f;

D3DXVECTOR3 vecSize = D3DXVECTOR3(Width, Height, Depth);

D3DXVECTOR3 vecHalf = vecSize * 0.5f;

vecLocalPoints[0] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[1] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[2] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[3] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[4] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[5] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[6] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[7] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);

If that small bit earlier is put into the main() function then it works fine. Am I not allowed to define an array outside of a function?

Share this post


Link to post
Share on other sites
Quote:
Original post by bengaltgrs
Ok, this is what I am actually trying to do:

D3DXVECTOR3 vecLocalPoints[8];

D3DXVECTOR3 vecWorldPoints[8];

float Width = 2.0f;
float Height = 2.0f;
float Depth = 2.0f;

D3DXVECTOR3 vecSize = D3DXVECTOR3(Width, Height, Depth);

D3DXVECTOR3 vecHalf = vecSize * 0.5f;

vecLocalPoints[0] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[1] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[2] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[3] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[4] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[5] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[6] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);
vecLocalPoints[7] = D3DXVECTOR3(-vecHalf.x, vecHalf.y, -vecHalf.z);

If that small bit earlier is put into the main() function then it works fine. Am I not allowed to define an array outside of a function?


You cant fill in the array like that outside a function, you would need to use the D3DXVECTOR3 vecLocalPoints[] = {D3DXVECTOR(...), ...}; syntax.

Share this post


Link to post
Share on other sites
C/C++ are procedural languages which means that execution proceeds from one function to another until some logic means that it leaves main(). Anything outside of a function can't be executed, except...

One of the only ways to get functions called outside of main is to use global objects. They are created before the main entry point is entered. If there are things done in the constructors you can execute procedural code there.

For example you could make a class whose sole purpose is to fill an array with values. If you create a global object of that type, passing it your global array then it will be initialised before main is entered. You need to be careful because the order of creation of objects between translation units is not guaranteed.

All of that is pretty much outside of normal programming practice and isn't a clever thing to do... it's tricky and I don't recommended it at all.

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