Sign in to follow this  
am2pm

c++ syntax

Recommended Posts

Hello gamedev community. I'm starting to learn openGL with c++ and have some problems. Since now, i've programmed for about 1.5 years in java - but it became boring, coz of disability making' the 'real' games. So i've decided to change my 'profile' and learn c++ and opengl. That means - the oop and general programing paradigms aren't strange for me - but i have some problems to 'check' the syntax (and maybe in some ways the semantics) of c++. After this 'fascinating' history - thats my Problem i'm sitting the whole day at:
const int worldi = 10;
const int worldj = 10;
const float areaSide = 4.0;

typedef float Vector[3];

typedef struct{
		Vector position;
		Vector normals[4]; 
		Vector coord[4];
		float height;
} area;

area world[worldi][worldj];

GLvoid InitWorld(){
	area tmpArea;
	for(int i = 0; i < worldi; i++){
		for(int j = 0; j < worldj; j++){
			tmpArea = world[i][j];
*********************	tmpArea.position = {float(i * areaSide), 0.0, float(j * areaSide)};

(...)

		}
	}
}

The debugger says: Syntax error :'{' in the, with stars marked, line... The next error is: Systaxerror: There's no ';' before { - also in the same line... I checked c++ tutorials and other things - sytax seems to be correct, but i dunno what it is... position is a Vector - means: 3-float-item-array. Init of an array in that way int bla[3] = { 1, 2, 3} is correct... Is there a problem with the call-by-refference/value concept? Huh... java was a bit easier to learn ;) Thanks for any kind of help... edit: of course theres some code before and after this section ;) edit2: the tags - sorry ;)

Share this post


Link to post
Share on other sites
hey,
when you post code like that it would be nice to use the source tags. This is what your code looks like with source tags:




const int worldi = 10;
const int worldj = 10;
const float areaSide = 4.0;

typedef float Vector[3];

typedef struct{
Vector position;
Vector normals[4];
Vector coord[4];
float height;
} area;

area world[worldi][worldj];

GLvoid InitWorld(){
area tmpArea;
for(int i = 0; i < worldi; i++){
for(int j = 0; j < worldj; j++){
tmpArea = world[i][j];
********************* tmpArea.position = {float(i * areaSide), 0.0, float(j * areaSide)};

(...)

}
}
}



To use these source tags, just do [ source] and [/ source], but don't do the spaces like I did. This makes your code A LOT more easier to read and understand.

Chad.

Share this post


Link to post
Share on other sites
const int worldi = 10;
const int worldj = 10;
const float areaSide = 4.0;

typedef float Vector[3];

typedef struct{
Vector position;
Vector normals[4];
Vector coord[4];
float height;
} area;

area world[worldi][worldj];

GLvoid InitWorld(){
for(int i = 0; i < worldi; i++){
for(int j = 0; j < worldj; j++){
world[i][j].position[0] = float(i * areaSide);
world[i][j].position[1] = 0.0;
world[i][j].position[2] = float(j * areaSide);

(...)

}
}
}



The few problems were that when you create a variable called "area tempArea" it makes a copy of the value, it is not a reference like in Java, so it will not change "world" at all. Remember this well. The second was you have to assign each component of the vector individually.

Share this post


Link to post
Share on other sites
I think you can only initialise arrays like that if you're declaring the variable on the same line.

I.e.
int[] intVec = {1, 2, 3}; // OK
Vector vec = {1.0F, 1.5F, 2.0F}; // probably OK
intVec = {4, 5}; // not OK


As an aside, I think your syntax for casting is wrong; it should be
(float)(i * areaSide)

... and besides, those values can be implicitly converted to float so you don't need to cast them at all.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob Janova
I think you can only initialise arrays like that if you're declaring the variable on the same line.

I.e.
int[] intVec = {1, 2, 3}; // OK
Vector vec = {1.0F, 1.5F, 2.0F}; // probably OK
intVec = {4, 5}; // not OK


As an aside, I think your syntax for casting is wrong; it should be
(float)(i * areaSide)

... and besides, those values can be implicitly converted to float so you don't need to cast them at all.


Exactly. Though the casting is correct, there is C-like casting and functional casting which is what is shown.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bob Janova
As an aside, I think your syntax for casting is wrong; it should be
[code](float)(i * areaSide)
... and besides, those values can be implicitly converted to float so you don't need to cast them at all.


That casting syntax was also a bit strange for me, but i saw it in one of the nehe tutorials and i took it so.
I made the casts only because of the debug error - i thought maybe there could be the reason for the errors...


Quote:
Original post by Bob Janova
I think you can only initialise arrays like that if you're declaring the variable on the same line.


aha... strange but k...


Thanks anyone for the help - this community is really amazing. It couln't be faster ;)

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