Header files in C++
Fasine

Greetings, everyone! I'm new to the forums (and also to Allegro) and have a few questions regarding problems with header files in C++. But first, some background: I got a "Program games with C++ and Allegro" book and killed all 900 pages of it in about three days, realizing that I really needed a refresher course in C++ (I took two semesters of it, but that was a year ago) so I got out my old textbook, opened up the .doc's from my lectures, and started reading through my old programs to get a feel for it again. I finally decided to compile one of my old programs that wrote to a .txt file, so I could compare what ended up in the file with the code I'd written, and lo and behold I got a compiler error! Now I used to use Borland and I'd opened the .cpp file in Dev-C++ (which was used extensively in the Game Programming book, so I decided to give it a try) so I thought perhaps that was the problem, but the error was listed as being in the header file (<fstream.h>), and looked so: `_RWSTDExport' does not name a type

//This is the code
#ifndef _RWSTD_STRICT_ANSI
extern const int _RWSTDExport __rwOpenTable[32];//this line was flagged.
#else
extern const char _RWSTDExportFunc(*) __rwOpenTable[32];
#endif

Along with that were about 20 other errors, saying things were declared improperly and such. So I decided to ignore it and proceed to try it in Visual C++ 2005 (I have 4 compilers, the 3 mentioned plus Crimson Editor). I got a compiler error there, too, also in the header file. This one said:
warning C4068: unknown pragma

//about this code...
#pragma option push -b -a8 -pc Vx Ve -w-inl -w-aus -w-sig

And also said it failed to open a header file named: stdcomp.h. I then tried compiling it in Borland and had no problems at all. I have to assume that when two different compilers are failing to use their own header files correctly, that it must be a result of user error on my part, so... any suggestions? Can anyone tell me how to make one or both of these compilers work. Also: can anyone point me to a resource that tells me how to create an Allegro library that's appropriate for Borland (which I'm more used to). And also: does anyone know of a good web resource/forum post pertaining to syntax and such in header files? I understand the basics, but I can't look at a header file and tell what it does to/in my program.

One more, totally unrelated question: I was messing around in a sample program the other night and had problems where by clicking left arrow once (turn left) I would turn more than 90 degrees if I held it longer than an instant. I tried fixing it with: void set_keyboard_rate(int delay, int repeat) but it had no effect at all, even using values as high as 1000. Can anyone tell me how to either A: use that function properly, or B: lower the keyboard rate in another way? Thanks a ton for your replies, everyone, and I apologize for the long post!

--Cheers, Fasine

gnolam
Quote:

<fstream.h>

I think you'll want just #include <fstream>.

Quote:

One more, totally unrelated question: I was messing around in a sample program the other night and had problems where by clicking left arrow once (turn left) I would turn more than 90 degrees if I held it longer than an instant. I tried fixing it with: void set_keyboard_rate(int delay, int repeat) but it had no effect at all, even using values as high as 1000. Can anyone tell me how to either A: use that function properly, or B: lower the keyboard rate in another way? Thanks a ton for your replies, everyone, and I apologize for the long post!

set_keyboard_rate() only affects the keystroke buffer (the one that's read with readkey()). The key[] array, however, is unaffected - it simply shows the keyboard's current state. So if your code looks like this:

if (key[KEY_LEFT])
    turn_left();

turn_left() will be called every cycle as long as KEY_LEFT is being held down. You will have to set your own variables to make sure it doesn't repeat. Example:

if (key[KEY_LEFT] && (left_delay == 0)) {
     turn_left();
     left_delay = 10;
    }

if (left_delay > 0)
    left_delay--;

Kitty Cat
Quote:

You will have to set your own variables to make sure it doesn't repeat.

No you don't.

while(keypressed())
{
   int k = readkey() >> 8;
   if(k == KEY_LEFT)
      turn_left();
   else if(k == KEY_RIGHT)
      turn_right();
   else...
}

I really wonder why people contantly try to use the key[] array (which gives key states) when they want key presses (key actions). Two different concepts.

Especially with the array, if the key is pressed quick enough, your program may not react quick enough and key[FOO] changes may be missed. With readkey, it'll always get to you.

If you want to see if a key is down or not, use key[]. If you want to see if a key was pressed, use readkey.

Evert

Sometimes you want to respond to key presses only when the key has been released, in which case readkey() is completely useless. Point well made that it depends on what you want to do wether key_pressed()/readkey() or key[] is appropriate.

Indeterminatus
Quote:

#pragma option push -b -a8 -pc Vx Ve -w-inl -w-aus -w-sig

Quote:

Can anyone tell me how to make one or both of these compilers work.

You'd have to open one of the compiler's documentation and look the correct #pragma option up. The thing is, that #pragma is pretty much left to the compiler, as defined in ISO C++ (ISO IEC 14882 1998, if you need to look it up), and thus is not guaranteed to work with all compilers. (Which, on another note, it isn't anyway, because not all compilers fully stick to the standard themselves)
Alternatively, stick to standard C++ (and thus, remove the #pragma). That should increase your chances of portability.

edit: Small factual correction.

Thread #589564. Printed from Allegro.cc