Archived

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

Northchild

Quick beginner's question about function calls (C++/VS NET)

Recommended Posts

Hi all, I''m making my first simple console program and was wondering how I would go about calling a function from another file. The file that I''m working on is titled main.cpp, and has a few functions. I''m putting class definitions in their own files and including them with #include in main.cpp. If I keep writing functions, main.cpp is going to be fairly cluttered. Thanks!

Share this post


Link to post
Share on other sites
So main() can call a function called game() in another cpp file associated with the VS solution? No need for a reply, I can try these small things myself. Just trying to piece stuff together.

Does the arrangement of various files in a project play a large role when you get to programming for the real world? Looking at some of the programs out there, it''s amazing to me to see that they have hundreds or even thousands of files. I''ll have to do a search to figure out what some of the more common file types are. Thanks again.

Share this post


Link to post
Share on other sites
quote:
Original post by Northchild
Does the arrangement of various files in a project play a large role when you get to programming for the real world?



Yes. Most definitely. Many "real world" projects exceed millions of lines of code, i think my eyes would implode if i saw more than 100,000 lines of code in one file Theres an article in the resources section about how to organise code in multiple files, you may find it useful.

Share this post


Link to post
Share on other sites
Still confused...

How would I use multiple cpp files in my project? I''ve read over "Organizing Code Files in C and C++" here at GameDev, and know how to #include and use headers for class definitions, etc, but I''m still fuzzy about how I would do something like:

1. I have a main.cpp file for a Zork-esque text adventure. When the user selects "new game", I want to move to the first part of the game, which would be inside a house. I don''t want to put all the text and functions for the house inside main.cpp, and I don''t think that house.h included in main.cpp would be appropriate, (though I''m not 100 percent sure here). I tried just #include (ing) the source "house.cpp" within main.cpp, but the compiler didn''t seem to like that.

Thanks again!

Share this post


Link to post
Share on other sites
quote:
Original post by Northchild
Still confused...

How would I use multiple cpp files in my project? I've read over "Organizing Code Files in C and C++" here at GameDev, and know how to #include and use headers for class definitions, etc, but I'm still fuzzy about how I would do something like:

1. I have a main.cpp file for a Zork-esque text adventure. When the user selects "new game", I want to move to the first part of the game, which would be inside a house. I don't want to put all the text and functions for the house inside main.cpp, and I don't think that house.h included in main.cpp would be appropriate, (though I'm not 100 percent sure here). I tried just #include (ing) the source "house.cpp" within main.cpp, but the compiler didn't seem to like that.

Thanks again!


NorthChild, I'm still pretty new at this myself, but hopefully I can help.

Your MSVC++ will automatically compile any .CPP file that is included in your current project. You don't have to #include these files, they will be compiled and used automatically.

So with that, you would have your Main.CPP, and House.CPP files (for examples).

Main.CPP would #include Main.H
House.cpp would #include House.H

When you compile, the program will read Main.CPP, read in Main.H, compile those, and move on to House.CPP, etc.

You could have a function in House.cpp and use it in main.cpp.
Note that in the Example I have #included House.h in Main.cpp as well. You have to include the Header file for a function in any .CPP file that you will use that function.

Here is a small example...

******** MAIN.CPP ***********

      
#include <iostream>

#include "main.h"

#include "house.h"

using namespace std;

//Function Delcarations are in Main.H


void main()
{
cout<<"Running a Procedure in Main now"<<endl;
MainProc();
cout<<"Done Running Main Proc"<<endl;
cout<<"Entering the House Procedure Now"<<endl;
House();
cout<<"Returned from the House Procedure"<<endl;
}

//Function Definitions

void MainProc()
{
cout<<"This is a Procedure in Main.cpp!!!!"<<endl;
}

************* END MAIN.CPP *******************

************* MAIN.H **********************

                
//In the Header File, we include the Function Definition

//We can then include this header file in any .CPP file that

//needs this Procedure.


//Don't forget Inclusion Guards in ALL your Headers

#ifndef INC_MAIN_H
#define INC_MAIN_H


void MainProc( void );

#endif //INC_MAIN_H


************** END MAIN.H *******************


************* HOUSE.CPP ******************

                
//In House.cpp we could include the Functions for Displaying

//What is in your house. Remember that any .CPP file that will

//need the procedures here, you will have to include House.H


#include "House.H"

#include <iostream>

using namespace std;

//The Function Definition for House() is in House.H


void House()
{
cout<<"This Procedure is in the House.cpp file!!!!"<<endl;
}

************* END HOUSE.CPP ******************

*************** HOUSE.H *********************

          
//Inclusion Guard Again

#ifndef INC_HOUSE_H
#define INC_HOUSE_H


//Fuction Definition

void House( void );


#endif // INC_HOUSE_H



*************** END HOUSE.H ******************


Hope this Helps. I didn't actually compile it so If it doesn't work, let me know!

~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
edit: fixed code
edit: fixed code again =)
edit: third times the charm?
[edited by - Radagar on August 19, 2002 4:26:08 PM]

[edited by - Radagar on August 19, 2002 4:28:43 PM]

[edited by - Radagar on August 19, 2002 5:40:33 PM]

Share this post


Link to post
Share on other sites
Hi all,
Forgive more newbie questions, but I have a couple that I hope will make things a bit clearer.

First:
What the heck are inclusion guards? What kind of weapons do they have? Err....nevermind the poor attempt of humor.

quote:

//Don''t forget Inclusion Guards in ALL your Headers
#ifndef INC_MAIN_H
#define INC_MAIN_H




The other question might seem like a basic question, but why do you need to do the #include main.h in the house.cpp? You don''t seem to be calling any functions in the main.cpp from the house.cpp. To me that seems that it is not needed. Or could it be just good practice to do so?

Thanks,
Triston

...and life is but a dream.

Share this post


Link to post
Share on other sites
quote:
Original post by Triston
Hi all,
Forgive more newbie questions, but I have a couple that I hope will make things a bit clearer.

First:
What the heck are inclusion guards? What kind of weapons do they have? Err....nevermind the poor attempt of humor.

//Don't forget Inclusion Guards in ALL your Headers
#ifndef INC_MAIN_H
#define INC_MAIN_H


The other question might seem like a basic question, but why do you need to do the #include main.h in the house.cpp? You don't seem to be calling any functions in the main.cpp from the house.cpp. To me that seems that it is not needed. Or could it be just good practice to do so?

Thanks,
Triston

...and life is but a dream.



Your Second Question first..

From Main.CPP I called the House() Function which does exist in House.CPP. That's why I needed to Include House.H. You are right when you say that you only need to include the Header file if you use functions from that file.

Now for your first question..

Inclusion Guards are usually equipped with Halberd's and longswords.. oh.. I mean..

Inclusion Guards keep a Header file from being read in more than once by your compiler. EVERY Header file needs an Inclusion guard, or you will have Linking or Compiling errors when you build your project.

Here is what an Inclusion Guard looks like...


#ifndef NAME_OF_INCLUSION
#define NAME_OF_INCLUSION

... Header File Goes Here ...

#endif // NAME_OF_INCLUSION (Comment)


Here is what it means.

the first command #ifndef, means IF NOT DEFINED. The compiler checks to see if NAME_OF_INCLUSION has been defined yet. If it hasn't, it goes on. If it has, then it skips down to the #endif.

the second command #define, does just that. It defines the NAME_OF_INCLUSION

the #endif simply ends the Inclusion Guard IF Statement.

the NAME_OF_INCLUSION is simply another variable name and can be anything. It's common practice to make it the filename, like this...


#ifndef HOUSE_H
#define HOUSE_H

... HOUSE_H Header Stuff Here ...

#endif


So the first time the compiler reads House.H, HOUSE_H isn't defined, so it defines it, reads in all the header stuff, and ends. The next time it reads that header file, HOUSE_H is already defined, so it skips to the #endif and doesn't re-read in the header info. This keeps you from having Multiple definitions of functions, which your Linker will complain about.

Hope I'm making sense!


~~~~~~~~~~~
Chris Vogel
~~~~~~~~~~~
edit - typo

[edited by - Radagar on August 20, 2002 10:05:39 AM]

Share this post


Link to post
Share on other sites