Thread: Divide code into h files ?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    40

    Divide code into h files ?

    Dear all , My code is too larg in one file, how can I divide the code in another h or any file else, and then include that file in the main program ?
    Last edited by GSalah; 11-13-2006 at 08:22 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    Thank you , I did it , but I have a problem. there is global variables in the main program , after this division. the h file compile error of undeclared identifier.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by GSalah
    the h file compile error of undeclared identifier.
    The sounds more like a linker error. Please try to be specific. Copy and paste is quite helpful, if possible.

    Try to extern the global variable declarations in the header, and then define them in exactly one source file.

    http://c-faq.com/decl/decldef.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    First of all, avoid using global variables. There are times when they are the best option, but you have to be sure before using them.

    Second, if there is an undeclared identifier in your .h file, then you probably did not include the right standard headers before it (or in it but before the identifier). You should not be declaring variables in header files.

    Third, the proper way to share global variables across files, is to use the extern key word. Extern is a variable identifier (like unsigned) that signifies that a variable is defined as a global variable in another file. You can put such a variable "deleration" in the header file to make it easy for all files to use it.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    sorry , the problem here with those global variables

    Code:
    //
    //
    // the main program file  :scnner.cpp
    //
    //
    
    
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include "scan_bin.h"
    
    
    using namespace std;
    
    
     ifstream input("c:\\compiler_test\\Data_file.txt"); // the code text file
     ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file
    
     
    //this for RESERVED_WORDS
    const int reserved_size=40;
    typedef char string[20] ; // array holds the reserved word
    string reserved[reserved_size]; // holds reserved_size reserved words
    int reserved_ID[reserved_size]; // holds the corsponding ID for each reserved word
    
    
    //this for SYMBOLS
    const int symbols_size=40;
    typedef char symbol[3]; //max symbol length is 2 characters
    symbol symbols_list[symbols_size];
    int symbols_ID[symbols_size];
    
    
    //the following ID list set by the functio set_ID()
    int Names_ID; // the ID of the names
    int Reserved_ID; // the starting ID for reserved_ID list
    int Symbols_ID; // the starting ID for the symbol_ID list
    
    
    // separators list
    // 10 the end line , 32 the space char
    // you can add new separator symbol here
    char sep_list[] = { 10,32, '.', ',', ';', ',', '+', '-', '='};
    const int is_Sep(char) ;  // returns 1 if in the sep_list ,0 if not
    
    
    //
    int main(){
    
    scan_bin();  // get all the information needed
    
    return 0;
    }

    here is the scan_bin.h file

    Code:
    
    
    //
    //  file : scan_bin.h
    //
    
    
    #ifndef	SCAN_BIN_H
    #define SCAN_BIN_H
    
    
       void scan_bin();
       void set_ID();
       void get_Reserved();
      void get_Symbols();
    
    
    #endif
    and here the file scan_bin.cpp

    Code:
    //
    // file : scan_bin.cpp 
    //
    
    #include "scan_bin.h"
    
    
    
    void scan_bin(){ 
    // this function scans the bin file to get all the information inside
     int row=0;
     int col=0;
     char ch;
     char temp[20];
    
     while(!bin_file.eof()) {
        
           bin_file.getline(temp ,40);
        	
           if(!strcmp(temp , "[RESERVED_WORDS]"))
                get_Reserved();  // calling the reserved words scanning function
    
            if(!strcmp(temp , "[SYMBOLS]"))
                get_Symbols();  // calling the reserved symbols scanning function
    				  
            if(!strcmp(temp , "[ID]"))
               set_ID();     // this function sets all the data related to the IDs
      }
    
    
    }
    void get_Symbols(){
     //  the symbols scanned here should be under SYMBOLS tag in bin_file
     // and saved the corsponding ID for each word in reserved_id array
     // if new tag bracket faced , the function will exit
    
     int row=0;
     int col=0;
     int crnt_read; // saves the read postion
     char ch;    // temporary char for scanned character
     char temp[3]; // temporary array for scanned symbol
    
      while(!bin_file.eof())
      { bin_file.get(ch);
    	while ( ch!=10 & !input.eof() & ch!=-1)
                     {  if( ch=='[' | ch==']')
    	         {bin_file.seekg(--crnt_read);
                                                return;}
    	   temp[col++]=ch;
                       crnt_read = bin_file.tellg();
    	   bin_file.get(ch); }
    
    	temp[col] = '\0';
    	strcpy(symbols_list[row] , temp);  // copying the word into the list
            col=0;
           row++;  }
    
    }
    //----------------------------------
    
    void get_Reserved()
    {//  the tokens scanned here should be under RESERVED_WORDS tag in bin_file
     // and saved the corsponding ID for each word in reserved_id array
     // if new tag bracket faced , the function will exit
     int row=0;
     int col=0;
     int crnt_read;
     char ch;    // temporary for scanned character
     char temp[20]; // temporary for scanned word
    
      while(!bin_file.eof())
      { bin_file.get(ch);
    
          while ( ch!=10 & !input.eof() & ch!=-1)
           {  if( ch=='[' | ch==']')
               {bin_file.seekg(--crnt_read);
                       return;}
             temp[col++]=ch;
             crnt_read = bin_file.tellg();
             bin_file.get(ch); }
    
            temp[col] = '\0';
            strcpy(reserved[row] , temp);  // copying the word into the list
            col=0;
           row++;}
    
    }
    
    //------------
    
    void set_ID(){
      //here we get the ID for each tokens type that under [ID] tag
     // if you want to add a tag , you should add it in the ID list
        int col=0 ;
        int row= 0;
        int index;
        char temp[20];
        int ID_value=0;
        char ch;
        char line[51];
    
    	 while(!bin_file.eof())
    	{ bin_file.get(ch);
    	    while (  !input.eof() & ch!=-1 &ch!=10)
    	      {  if( ch=='[' | ch==']')
    	            break;
    	            line[col++]=ch;
    	            bin_file.get(ch); }
    
    	      line[col] = '\0';
    	      for(index = 0 ;line[index]!='=' ; index++)
    	       temp[index] = line[index];
    
    	      temp[index++]='\0';
    	       ID_value=0;
          // converting to decimal number
    	      for(; line[index]!='\0' ; index++)
    	         {ID_value*=10;
    	          ID_value+=line[index] - 48;}
    
    	col=0;
    	row++;
    	// assigning values to ID list
    	if(!strcmp(temp,"RESERVED_WORDS"))
    		Reserved_ID = ID_value;
    	else if(!strcmp(temp,"SYMBOLS"))
    	          Symbols_ID=ID_value;
                    else if(!strcmp(temp,"NAMES"))
    	          Names_ID=ID_value;
    
      }
    
    // setting the corrospnding reserved_ID values
    for(index =0 ; index <reserved_size ; index++)
    {reserved_ID[index] = Reserved_ID + index;
     symbols_ID[index] =  Symbols_ID + index;
    }
    
    }
    thank you so much

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You need to add stuff like this to the header.
    Code:
    #include <fstream.h>
    extern ifstream bin_file;
    extern ifstream input;
    Only a start. But you ought to consider standard headers.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    when should I add them, In the main file

    and , did you mean ?
    Code:
    extern ifstream input("c:\\compiler_test\\Data_file.txt"); // the code text file
    extern ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by GSalah
    when should I add them, In the main file

    and , did you mean ?
    Code:
    extern ifstream input("c:\\compiler_test\\Data_file.txt"); // the code text file
    extern ifstream bin_file("c:\\compiler_test\\bin.txt"); // compiler information file
    No. What I posted was declarations -- they go in a header. Definitions go in exactly one source file. And they already appear to be in the main file.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    The same error. sorry I think I don't understand you clearly. In all cases . If I past them in scan_bin.cpp ; 24 errors . If I past them in scan_bin.h or main.cpp the same previous errors a

    sorry . please can you modify the code !

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I've been trying to show you how to do this yourself, as it is likely to come up more than this once.

    Using string as a typedef does not help you at all. I've renamed it qqq -- give it whatever name you would prefer (other than string).

    As I said, "[y]ou need to add stuff like this to the header." That meant that there was more to look at. This might be a more complete list.
    Code:
    #include <fstream>
    using std::ifstream;
    
    extern ifstream bin_file;
    extern ifstream input;
    
    //this for RESERVED_WORDS
    extern const int reserved_size;
    typedef char qqq[20] ; // array holds the reserved word
    extern qqq reserved[]; // holds reserved_size reserved words
    extern int reserved_ID[]; // holds the corsponding ID for each reserved word
    
    
    //this for SYMBOLS
    typedef char symbol[3]; //max symbol length is 2 characters
    extern symbol symbols_list[];
    extern int symbols_ID[];
    
    
    //the following ID list set by the functio set_ID()
    extern int Names_ID; // the ID of the names
    extern int Reserved_ID; // the starting ID for reserved_ID list
    extern int Symbols_ID; // the starting ID for the symbol_ID list
    
    
    // separators list
    // 10 the end line , 32 the space char
    // you can add new separator symbol here
    extern char sep_list[];
    extern const int is_Sep(char) ;  // returns 1 if in the sep_list ,0 if not
    But don't ask me why you have issues with qqq if you copy and paste -- make the corresponding change(s).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    Nov 2006
    Posts
    40
    thank you so so so so much
    Its clear now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. Large exe files for small amounts of code problem
    By rainmanddw in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2003, 05:52 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. header files and code files..
    By CompiledMonkey in forum C++ Programming
    Replies: 4
    Last Post: 02-15-2002, 09:35 AM