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 ?
Printable View
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 ?
FAQ > How do I... (Level 2) > Multiple source files for one program (C++ example)
Adapt to your environment/system.
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.
The sounds more like a linker error. Please try to be specific. Copy and paste is quite helpful, if possible.Quote:
Originally Posted by GSalah
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
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.
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
and here the file scan_bin.cppCode:
//
// 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
thank you so muchCode:
//
// 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;
}
}
You need to add stuff like this to the header.
Only a start. But you ought to consider standard headers.Code:#include <fstream.h>
extern ifstream bin_file;
extern ifstream input;
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.Quote:
Originally Posted by GSalah
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 !
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.
But don't ask me why you have issues with qqq if you copy and paste -- make the corresponding change(s).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
thank you so so so so much :)
Its clear now.