Thread: multiple file use

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    224

    multiple file use

    ok im trying out the concept of splitting your program into multiple source files. i have 2 files main and loadmap

    in main.cc I have

    void loadmap(int);

    in loadmap.cc i have
    Code:
    void loadmap(int map)
    {
         . 
         .
         .
    }
    then when i try to use loadmap() in the file main.cc by going loadmap(map) (map is an int) and that doesnt work and i tried loadmap(1) and that didnt work so im guessing i need a header file somewere. but i dont know what to put in it. I also dont know the layout for a header.

    any help is appreciated.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always look at the FAQ entry.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Code:
    // loadmap.h
    #ifndef LOADMAP_H
    #define LOADMAP_H
    
    void loadmap(int);
    
    #endif
    You don't really need the preprocessor stuff in this case, but it's a good habit to get into for your header files.
    Code:
    // loadmap.cc
    #include "loadmap.h"
    
    void loadmap(int map)
    {
      // Code here
    }
    Code:
    #include "loadmap.h"
    
    int main()
    {
      // Call loadmap here
    }
    Kampai!

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    There are a couple of ways to approach this. First is to use the keyword extern in the function declaration, this will inform the linker that the functions are declared in another file. An example of this is below:

    in main:
    Code:
    #include <iostream>
    
    using std::cin;
    
    extern int getInfo(void); //function declaration
    extern void displayHello(void); //function declaration
    
    int main() {
    
    	displayHello(); //call our first function
    	cin.get(); //keep window open
    
    	return 0;
    }
    in my other file:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int getInfo(void) {
    
    	int numTimes;
    
    	cout << "How many times? ";
    	cin >> numTimes;
    
    	return numTimes;
    }
    void displayHello(void) {
    
    	int maxTime;
    
    	maxTime = getInfo();
    
    	for(int i = 0; i < maxTime; i++) {
    		cout << "Hello" << endl;
    	}
    }
    Now however this approach works it is better for the logical layout of your program to use header files. Basically a header file simply contains the function declarations whereas the .cpp file contains the function definitions. You would create the header file and then include that in both your source files like so:

    in the header file:
    Code:
    #ifndef myHeader_h
    #define myHeader_h
    
    
    int getInfo(void); //function declaration
    void displayHello(void); //function declaration
    
    #endif
    in the source file:
    Code:
    #include <iostream>
    #include "myHeader.h"
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int getInfo(void) {
    
    	int numTimes;
    
    	cout << "How many times? ";
    	cin >> numTimes;
    
    	return numTimes;
    }
    void displayHello(void) {
    
    	int maxTime;
    
    	maxTime = getInfo();
    
    	for(int i = 0; i < maxTime; i++) {
    		cout << "Hello" << endl;
    	}
    }
    and finally in main:
    Code:
    #include <iostream>
    #include "myHeader.h"
    
    using std::cin;
    
    
    int main() {
    
    	displayHello(); //call our first function
    	cin.get(); //keep window open
    
    	return 0;
    }
    the preprocessor directives #ifndef, #def, and #endif just ensure that the header file is only linked to once.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    alright thanks for the quick replies.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    BTW whats .cc mean .c is for c .cpp is for c++

    oh and i also still get this error message
    Code:
    :undefined reference to 'loadmap(int)'
    after doing what you said.
    Last edited by c++.prog.newbie; 02-05-2005 at 05:11 PM.

  7. #7
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>BTW whats .cc mean .c is for c .cpp is for c++
    .cc is also used by some compilers for C++. Another extension you may see is .C
    Kampai!

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    after doing what you said.
    After doing what exactly? Post the relevant code.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by c++.prog.newbie
    ok im trying out the concept of splitting your program into multiple source files. i have 2 files main and loadmap

    in main.cc I have

    void loadmap(int);

    in loadmap.cc i have
    Code:
    void loadmap(int map)
    {
         . 
         .
         .
    }
    then when i try to use loadmap() in the file main.cc by going loadmap(map) (map is an int) and that doesnt work and i tried loadmap(1) and that didnt work so im guessing i need a header file somewere. but i dont know what to put in it. I also dont know the layout for a header.

    any help is appreciated.

    Your file organization seems OK to me. How did you compile and link?

    For example, with GNU g++

    g++ -o main main.cc loadmap.cc


    Then execute main

    For Borland or Microsoft compilers, the simplest way is to rename the files main.cpp and loadmap.cpp and then do this

    Borland:
    bcc32 main.cpp loadmap.cpp

    Microsoft:
    cl main.cpp loadmap.cpp /GX

    Regards,

    Dave

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i think i forgot to link loadmap.cc but ill double check

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    ya i forgot to link loadmap.cc but now i get a whole bunch of errors such as cout undeclared first use this function whether i have iostream included in the file or not

  12. #12
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    including iostream is not enough to use cout or cin or anything else in the header for that matter. All these objects and functions are declared inside the std namespace. This is the faq on namespaces .

    As you can see a namespace is essentially a user defined scope and as such in order to access anything within that namespace you need to bring that scope into focus. One of the ways to do that with the std namespace is to have:
    Code:
    using namespace std;
    At the begginning of your file after the includes to bring the std namespace into global scope. Another option is to just bring the objects you will be using into scope, such as say you only wanted to deal with cin, cout and endl. You would do that by placing:
    Code:
    using std::cin;
    using std::cout;
    using std::endl;
    And finally if you wanted to just use the objects without bringing the namespace into a larger scope you could just resolve the namespace as you went in your program:
    Code:
    #include<iostream>
    
    int main() {
    
             std::cout<<"This resolves the namespace for this object";
             std::cin.get();
             return 0;
    }
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    224
    i had using namespace std; in the main but not the loadmap.cc well in the process of movin over that past couple of days i decided that i am gunna scrap that idea and start an new one but im having this error though i must say that the help you provided helped for this revision

    Code:
     A:\Helgas_magic.cpp In function `void helga_magicshop()': 
    319 A:\Helgas_magic.cpp syntax error at end of input 
     A:\Makefile.win [Build Error]  [Helgas_magic.o] Error 1
    (the 319 is the line in the file)

    i got the files

    main.cpp
    helgas_magic.cpp
    magicshops.h

    tellme if you need more info or need to see whats inside. (im still teaching myself how to split programs

    EDIT: oh and if i need a variable to be used in the main.cpp and helgas_magic.cpp do i need to declare it in the .h ?
    Last edited by c++.prog.newbie; 02-11-2005 at 09:31 PM.

  14. #14
    Registered User
    Join Date
    Feb 2005
    Posts
    14
    Need to see the function void helga_magicshop()
    to see what the syntax error is

    Answer to edit - no if pass variable as a parameter to functions.

  15. #15
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Definitely need to see the function its flagging, but look for missing ';' and typos by any cin's(well anyplace you accept input from either the user or a file.)
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM