Archived

This topic is now archived and is closed to further replies.

ury2ok

c++ help

Recommended Posts

I know VB pretty well but am a beginner to c. i learnt the console version and not the windows version. My first question is based on the following statement. And there’s also a couple of header (.H) files: Utils.h contains a ‘description’, called a prototype or declaration, for each of the functions implemented in Utils.cpp. As long as any other source file has access to this header file, it can use the functions contained inside. so that means if i make a function in a .cpp file (lets say game.cpp) and i have another .cpp called functions.cpp i couldnt make a call from game.cpp and access the function that is in functions.cpp? So if i wanted to access this function i would have to stick it into a .h file and #include the file at the top of game.cpp? Thanks

Share this post


Link to post
Share on other sites
Normally, you can include the cpp file directly. I stick to h files though, because I haven''t learnt what''s good or bad with them, most use h files

Share this post


Link to post
Share on other sites
You cannot use a function prior to its _declaration_ "int foo();", not necessarily its _definition_ "int foo() { do stuff; }".
You must thus have declarations of functions with extern linkage in each source file. You could add them to each cpp file, but the easiest way is to stick them in a header file. Just like it is done with C library functions.

Share this post


Link to post
Share on other sites
Yes, and remember to use quotes instead of angle brackets when you #include your own header files.

^^^^^^^^^^^^^^^^^^^^^^^^^
Wait, wait, wait....who''s Nambla Fett?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
don''t include .cpp files, that duplicates the code of the function. By including headers instead the same function is used everywhere. It gets even worse if you are including variables in a cpp, then you have two copies of the varaible, they are not the same variable. That will ruin your program.

Share this post


Link to post
Share on other sites