• Create Account

# multiple source files and global varaibles.......................

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1LAURENT*  Members   -  Reputation: 290

Like
1Likes
Like

Posted 03 August 2014 - 01:44 PM

Hello all

Today I've decided it was time to upgrade my skills and use multiple source files instead of coding everything in one big cpp file. I've tried this before but I failed so hard for a week I quit in frustration and went along with my previous  method using only one file. Researching this topic and using 3 main tutorials which includes one on this site I've manage to bring the errors down to a minimum.

My problem is with my global variables which I was trained to utilize the most. What I've done is put every global variable in a header file and with the "extern"  key word, then I defined them in a cpp file name global.cpp. I have gotten these specific errors despite including the global header inside the main file.

Error C2466

Error C2133

Error C2466

Error C2057

Error C2133

I have followed an extremely similar pattern of working with multiple source files. I created a header for every class and a cpp for all the function in the class. I never included a cpp file. I used #ifndef, #define, #endif in every header file. All headers are included within main. I've done just about everything I can think of for the moment.

If you haven't noticed I code in c++ programming language, I use visual studious 2010 and that about it. If you have a little bit or a lot of bit of experience dealing with multiple source file I'm preparing a file which you can download and see my source code. I've done everything hy the book so it should be predictable.

https://www.dropbox.com/s/7b7yqei55svcyc1/Protype.zip

Maybe I'm trying too hard, Maybe I'm not cut out to be a programmer, I always have error and I can never completely figure out stuff on my own. Anyways here the folder if you desire more details on the problem.

Edited by LAURENT*, 03 August 2014 - 01:59 PM.

### #2aregee  Members   -  Reputation: 1072

Like
2Likes
Like

Posted 03 August 2014 - 05:22 PM

Compiler Error C2466: An array is allocated or declared with size zero.

Compiler Error C2133: 'identifier' : unknown size

Compiler Error C2057: expected constant expression

Doesn't the compiler tell you which line numbers you have problems with?  Seems like trivial things to fix to me, but I don't have time to look through your source code right now.  The error messages doesn't indicate any problems with include-files being included multiple times or something like that.  Seems like 'syntax errors' to me.

Edit: Don't think that you are not fit to be a programmer.  Everyone stumbles on barriers, and you just hit the number one way of learning - by erring!  When you sort this problem, you will come out as a slightly better programmer.  Every time you make an error that you fix you get a little tad better.

Edit 2: I will try to have a look tomorrow, if you haven't sorted your problem, or anyone else hasn't helped you.

Edited by aregee, 03 August 2014 - 05:27 PM.

### #3aregee  Members   -  Reputation: 1072

Like
1Likes
Like

Posted 03 August 2014 - 06:26 PM

I did have a look through your code, and I didn't immediately see anything.  Your code looks good as far as I can see from the quick look I had.  I think line numbers would help a bit more.

Just a few nitpicking though.

Indentation:

Be consistent.  Consider using 4 spaces rather than 8 for tabs.

Don't do this:

switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= YS_VEL_WALK; zVel -= YS_VEL_WALK; break;
case SDLK_DOWN: yVel += YS_VEL_WALK; zVel += YS_VEL_WALK; break;
case SDLK_LEFT: xVel -= YS_VEL_WALK; break;
case SDLK_RIGHT: xVel += YS_VEL_WALK; break;
case SDLK_SPACE: if(grounded == true){z -= 4; zVel -= YS_VEL_WALK + 20;} break;
}  

switch( event.key.keysym.sym )
{
case SDLK_UP:
{
yVel -= YS_VEL_WALK;
zVel -= YS_VEL_WALK;
break;
}
//ETC...
default:  //Always include the default clause (even if it is not an error not to).
{
break;
}
} 
Don't do this:

if( check_collision( camera, floorpad ) == true )
apply_surface( wallfront.x - camera.x, wallfront.y - camera.y, tileSheet, screen, &clips[ type ] );
//Hard to immediately tell that this next line is NOT part of the IF, especially when you have not indented the line that do belong.

Do this instead (even if the above is technically correct). (Don't be afraid of curly braces, even if you have only one line inside.)

if( check_collision( camera, floorpad ) == true )
{
apply_surface( wallfront.x - camera.x, wallfront.y - camera.y, tileSheet, screen, &clips[ type ] );
}

At least indent the second line so it is clear that it belongs to the IF (and the next line is not).

Be neat and consistent about spacing and indentation.  When you see a '{', always indent what is enclosed.  Use the closing '}' on the same line as the opening one.

Remember:  The key point is that the code shall be as readable as possible.

Other than that, I am quite impressed with what you have achieved, considering the lack of credit you are giving yourself.  You certainly understand quite a few important key points.  I wouldn't give up programming quite yet, if I were you.  At least not if you are thinking it is fun!

### #4aregee  Members   -  Reputation: 1072

Like
0Likes
Like

Posted 03 August 2014 - 06:45 PM

Oh found the log in your debug folder.  Seems it is complaining about two lines I thought of as possible candidates, but the consts seemed valid so I rejected that possibility:

main.cpp, line 25, 26:

Tile *tiles[ TOTAL_TILES ];
Tile *walls[ TOTAL_TILES_WALL ]; 
In your global.h file you have:

const int TOTAL_TILES = 192;
const int TOTAL_TILES_WALL = 1;


Seems ok to me...

The question is why the compiler thinks that TOTAL_FILES and TOTAL_FILES_WALL is '0'.

Edited by aregee, 03 August 2014 - 06:47 PM.

### #5LAURENT*  Members   -  Reputation: 290

Like
0Likes
Like

Posted 03 August 2014 - 08:12 PM

That the same question I asked. Why doesn't the compiler see the value? It should according to the instructions I've read. I really wish I could take the simple route and type the number in but I need to learn somehow.

I've been on and off all day figuring this out. The only thing I haven't tried yet is using break point in the debugger. Anyways thanks for the assistance I'm still not giving up I just got a little upset because I didn't get it the second time.

### #6shacktar  Members   -  Reputation: 1117

Like
3Likes
Like

Posted 03 August 2014 - 09:01 PM

Array sizes need to be known at compile time.

The problem here is that the lines here:

Tile *tiles[ TOTAL_TILES ];

Tile *walls[ TOTAL_TILES_WALL ];


do not know what the values of TOTAL_TILES and TOTAL_TILES_WALL actually are because they are defined in another compilation unit (they are defined in global.cpp but needed in main.cpp)

It is good practice to put

extern const int MY_GLOBAL;

const int MY_GLOBAL = 10;

in the cpp file. In cases with non-cost objects, this is necessary to prevent linker errors. However, in this case, this leads to the above confusion.

You could actually put

const int TOTAL_TILES = 192;
const int TOTAL_TILES_WALL = 1;


in global.h and remove their definitions entirely from global.cpp and it will compile for you. And it won't produce any linker errors because constants defined like this essentially disappear as their values are copied wherever necessary without them actually taking up a linker symbol.

Edited by shacktar, 03 August 2014 - 09:12 PM.

### #7Artemiye  Members   -  Reputation: 1053

Like
0Likes
Like

Posted 03 August 2014 - 09:12 PM

That the same question I asked. Why doesn't the compiler see the value? It should according to the instructions I've read. I really wish I could take the simple route and type the number in but I need to learn somehow.

I've been on and off all day figuring this out. The only thing I haven't tried yet is using break point in the debugger. Anyways thanks for the assistance I'm still not giving up I just got a little upset because I didn't get it the second time.

If you really want to do it as per your source code you'll have to include global.cpp in main.cpp.  Personally I think it'd be cleaner for Tile constants to be associated with the Tile class to allow easier access.  It'd be as simple as typing Tile::CONSTANT_NAME which is easier to use when compared to looking through an excessive amount of variables, constants, and whatnot in the global namespace.  This also allows you to break up constants into smaller, bite-sized relevant chunks so you don't have one massive file with tons of constants, which can get messy quick.

### #8jmakitalo  Members   -  Reputation: 668

Like
1Likes
Like

Posted 04 August 2014 - 12:49 AM

I recognize that you are still learning basic programming, but I still would like to give one design hint: don't use global variables. It will eventually lead to errors that are very difficult to trace and it will also make the code difficult to read. For instance, if I take a look at Function.cpp and its function clip_tiles(). It seems to reference the array clips, but it is not at all clear to the reader where this array is defined. Try to construct your functions so that they only act on the given arguments and have no reference to global things, except for global constants. Global constants are ok.

I remember the time when I was a beginner programmer and global variables seemed like an ok way to do things (I had also Basic background). I though it was impossible to write a big real life program without global variables. Boy was I wrong. It's very much possible to construct your game with something like

int main()
{
CGame game;

game.initialize();

while(game.isRunning()){
game.updateAwsomeness();
}

game.close();

return 0;
}


No globals needed, everything is contained in class CGame. Some might say that this is not the best approach either, but it is certainly an improvement to using a number of global variables.

### #9nobodynews  Crossbones+   -  Reputation: 2734

Like
1Likes
Like

Posted 04 August 2014 - 09:10 AM

My problem is with my global variables which I was trained to utilize the most.

I'm curious what you mean by this. I've only heard train and globals used together when saying you should train yourself to not use them.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

### #10LAURENT*  Members   -  Reputation: 290

Like
0Likes
Like

Posted 04 August 2014 - 10:18 AM

My problem is with my global variables which I was trained to utilize the most.

I'm curious what you mean by this. I've only heard train and globals used together when saying you should train yourself to not use them.

Tutorials I've studied tried to make concepts as simple as possible using global variables to teach. I became used to using them and developed many bad habits.

Array sizes need to be known at compile time.

The problem here is that the lines here:

Tile *tiles[ TOTAL_TILES ];

Tile *walls[ TOTAL_TILES_WALL ];


do not know what the values of TOTAL_TILES and TOTAL_TILES_WALL actually are because they are defined in another compilation unit (they are defined in global.cpp but needed in main.cpp)

It is good practice to put

extern const int MY_GLOBAL;

const int MY_GLOBAL = 10;

in the cpp file. In cases with non-cost objects, this is necessary to prevent linker errors. However, in this case, this leads to the above confusion.

You could actually put

const int TOTAL_TILES = 192;
const int TOTAL_TILES_WALL = 1;


in global.h and remove their definitions entirely from global.cpp and it will compile for you. And it won't produce any linker errors because constants defined like this essentially disappear as their values are copied wherever necessary without them actually taking up a linker symbol.

The code work fine but something doesn't feel right. It feels like I'm breaking rules. I guess I'll just create another header file full of defined constant since I don't need the cpp.

That the same question I asked. Why doesn't the compiler see the value? It should according to the instructions I've read. I really wish I could take the simple route and type the number in but I need to learn somehow.

I've been on and off all day figuring this out. The only thing I haven't tried yet is using break point in the debugger. Anyways thanks for the assistance I'm still not giving up I just got a little upset because I didn't get it the second time.

If you really want to do it as per your source code you'll have to include global.cpp in main.cpp.  Personally I think it'd be cleaner for Tile constants to be associated with the Tile class to allow easier access.  It'd be as simple as typing Tile::CONSTANT_NAME which is easier to use when compared to looking through an excessive amount of variables, constants, and whatnot in the global namespace.  This also allows you to break up constants into smaller, bite-sized relevant chunks so you don't have one massive file with tons of constants, which can get messy quick.

I heard including cpp wasn't the best. Anyways you're right I do have a lot of constants. I was planning to shrink my code using something call enum but this option seems efficient too

### #11Artemiye  Members   -  Reputation: 1053

Like
2Likes
Like

Posted 04 August 2014 - 08:31 PM

Anyways you're right I do have a lot of constants.

Yes, and I've tried the whole "keep the constants in one file thing" before a long time ago and I was not a fan of the results.

I was planning to shrink my code using something call enum but this option seems efficient too

I'd encourage you to try any method that you come up with.  Simply taking someone's word for something is not nearly as effective of a learning experience as trying something yourself and deciding that it isn't efficient.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS