Public Group

Some basic C++ questions

This topic is 4832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I know that the way C++ works is second nature to some, but I noticed some differences (apart from syntax) where C++ differs from JS and WDL. I was just hoping someone could clarify these for me. Namely, in most of the languages I've used, a function must be defined before it is called. Meaning that if ragnarok() is called from main(), ragnarok() must be defined above main(). However, in all of the references (namely "Teach Yourself C++ in 21 Days"), a function prototype is put above the main function, but the function itself is defined below it. This seems like a discrepancy to me. Can you define a function above main() as well? That was just bothering me. Come to think of it, that was the only issue I had. Apart from that, all I was looking to know is, does anyone know any good references to help learn Windows programming. For making games, I mean. Because right now, I'm working in DOS, but I want to work my way up. You know how it is. DOS...ech.

Share on other sites
Yes, you can define it above Main. If you think of it from a compiler's point of view, the compiler just simply needs to make sure the function exists and is being called with the right parameter types, etc. The compiler doesn't need to know how the function works until it gets around to actually compiling its code.

So, in short, yes, you can either define it above or put the prototype above.

As for beginning with Windows programming -- look at the tutorials on this site. They are an often overlooked, brilliant reference.

Share on other sites
Quote:
 Original post by hiroshisanCan you define a function above main() as well? That was just bothering me.

Yes you can, but eventually you'll just end up with multiple files anyway. It can also happens that A calls B and B calls A, which one would you put on the top then ? Better get used to it now.

Quote:
 Original post by hiroshisanCome to think of it, that was the only issue I had. Apart from that, all I was looking to know is, does anyone know any good references to help learn Windows programming. For making games, I mean. Because right now, I'm working in DOS, but I want to work my way up. You know how it is. DOS...ech.

Personally, I wouldn't advise you to learn any Windows programming in C++ with the current state of affairs. A lot of Windows API are being redesigned and much of them are quickly becoming obsolete. If you want to learn Windows programming, I suggest picking a language such as C# (Managed C++ could also be an option, but I for one really didn't like it). If you only want to make games, look into libraries like SDL, which not only have the advantage of being cross-platform but are also much easier to use.

Share on other sites
An identifier like a function must be declared before it can be used. That is what the prototypes are. It doesn't have to be defined, e.g using a function body, to be used, but must be provided during linking if it is a staticly linked. If it's put under the main() function it will be in the same translation unit and linked in. You could also have provided the function definition in another file and linked them together.

Note that a definition of a function is a also a declaration, so if you did define it above main() you wouldn't need a separate declaration.

Share on other sites
And to go a step further, if you were to declare a variable in a separate object file (which would be a compiled *.cpp), and you want to access it, you would have to tell the compiler that it exists also. To do this, you use the "extern" keyword. Example:
<Bob.cpp>bool BobEatsCheese=true;<George.cpp>extern bool BobEatsCheese; //Tell the compiler it is there somewhere.

NOTE: If you use the "extern" keyword and call it in your code, yet it is not in an object file, a linker error will be returned.

Share on other sites
Also, just to add one more note on the matter, a lot of compilers will accept code where you have a function prototype but no definition later in the code. It'll compile, but it sure won't run properly - keep an eye out for that one, I tripped myself up once by putting in all the prototypes first and then forgetting one of the functions. [embarrass]

Share on other sites
I think I get it. Defining the function is kind of flexible as to location because, since it is being compiled, it will be found one way or another?

And is Windows programming a bad idea at the moment? I wanted to get into sprite-based stuff (side scrollers, maybe overhead shooters, or even something more simple than that). Anybody know any tutorials for that kind of thing. Even just knowing how to make tetris would be a start. I mean I'd love to do 3D stuff, but I can't even imagine at this point how I would even get the code to interpret anything I've made in 3D Studio Max.

So are there any comprehensive tutorials on sprite-based gaming anywhere. Not necessarily for Windows, just as long as it's not DOS. I'm sick of DOS giving me that monochrome stare. It creeps me out.

Share on other sites
you can also make inline functions in c++ if you want too, they are always fun. with the definition inside the prototype. fun times! :):)
lol and if you dont like all the non color fun of dos, use msdos! hahah the command "color" makes every command saturated in colory goodness! (i like color 0a) black background with green typing... MATRIXIFIED!!! hehehe

Share on other sites
I'd recommend you try one of the simpler 2d libraries such as SDL, Allegro or ClanLib.

You can find some links to some good Allegro and SDL tutorials here, otherwise just Google for some.

Share on other sites
I'm actually just downloading the newest version of Allegro, because I went today and got a book...Game Programming All-in-One, 2E. It seems like pretty old software, but the principles should be about the same. I think.

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• Forum Statistics

• Total Topics
632170
• Total Posts
3004550

×