Separating code into header, functions, main files causes compile errors.

I am trying to separate my code into header, function, and main function files. The program compiles and runs to spec when it is all in the one file, but when separated out I get the same errors pointing to the start of each function:

hardware.h:28:23: error: variable or field ‘initialiseBinary’ declared void
void initialiseBinary(fstream &file);

hardware.h:28:23: error: ‘fstream’ was not declared in this scope

hardware.h:28:32: error: ‘file’ was not declared in this scope
void initialiseBinary(fstream &file);

I haven't encountered these problems before when working with separate files. I can't post all the code, so I'm posting as much as possible. This is an assignment so I'm looking for a direction rather than someone to simply solive it as I have been banging my head into this for hours.

test.cpp : All of the code in one file. No errors, compiles and runs to spec.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;

int const MAXSTRUCT = 100, NAMESIZE = 30;

struct Hardware
{
...
};

bool fileCheck(char fileName[]);

void initialiseBinary(fstream &file);

void partInput(fstream &outFile);

void partList(fstream &inFile);

void updatePart(fstream &upFile);

void insertPart(fstream &upFile);

void deletePart(fstream &upFile);

bool partSearch(fstream &inFile, int idNum);

void printMenu();


int main()
{
	fstream file;
	ofstream blankFile;
	char fileName[20] = "hardware.dat";
	char command;
	bool startComplete = false;

	if(!fileCheck(fileName))
	{//Checks if the file exists then creates and initialises as appropriate.
		cout << "File could not be opened, creating file." << endl;

		blankFile.open("hardware.dat");
		file.flush();
		file.close();
	
		file.open("hardware.dat", ios::in | ios::out | ios::binary);
		initialiseBinary(file);

		partInput(file);
	}
	else
	{//If the file is found, asks if the data should be cleared.
		while (command != 'n' && command != 'N' && !startComplete)
		{
			cout << "Do you wish to initialise the data file y/n? ";
			cin >> command;
	
			if(command == 'y' || command == 'Y')
			{//Opens and initialises all records to empty then invites input.
				file.open("hardware.dat", ios::in | ios::out | ios::binary);

				initialiseBinary(file);

				startComplete = true;
		
				partInput(file);
			}
			else if(command == 'n' || command == 'N')
			{//Opens and directs the user straight to the menu.
				file.open("hardware.dat", ios::in | ios::out | ios::binary);

				startComplete = true;
			}
			else
				cout << "Command not recognised." << endl;
		}
	}

	while (command != '5')
	{//Repeats the menu cycle until the quit command is given.
		printMenu();
		cin >> command;

		switch(command)
		{
			case '1':
				partList(file);
				break;
			case '2':
				updatePart(file);
				break;
			case '3':
				insertPart(file);
				break;
			case '4':
				deletePart(file);
				break;
			case '5':
				cout << "Thank you." << endl;
				break;
			default:
				cout << "Invalid command." << endl;
		}
	}

	file.close();

	return 0;
}


bool fileCheck(char fileName[])
//Checks if the file exists.
{
...
}


void initialiseBinary(fstream &file)
//Initialises the binary file with 100 structs.
{
...	 
}


void partInput(fstream &outFile)
//Gets user input and outputs it to structs in binary file.
{
...
}


void partList(fstream &inFile)
//Displays all the stored records;
{
...
}


void updatePart(fstream &upFile)
//Updates the information of a part.
{
...
}


void insertPart(fstream &upFile)
//Inserts a new record to the database.
{
...
}


void deletePart(fstream &upFile)
//Deletes a part.
{
...
}


bool partSearch(fstream &inFile, int idNum)
//Searches for a part based on ID number, returns true if found.
{
...	
}

void printMenu()
//Outputs command menu.
{
...
}


And here's an approximation of the separation without function content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//Header file

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

int const MAXSTRUCT = 100, NAMESIZE = 30;

struct Hardware
{
...
};

bool fileCheck(char fileName[]);

void initialiseBinary(fstream &file);

void partInput(fstream &outFile);

void partList(fstream &inFile);

void updatePart(fstream &upFile);

void insertPart(fstream &upFile);

void deletePart(fstream &upFile);

bool partSearch(fstream &inFile, int idNum);

void printMenu();


//Main function
#include "hardware.h"
using namespace std;

int main()
{
	fstream file;
	ofstream blankFile;
	char fileName[20] = "hardware.dat";
	char command;
	bool startComplete = false;

	if(!fileCheck(fileName))
	{//Checks if the file exists then creates and initialises as appropriate.
...
	}
	else
	{//If the file is found, asks if the data should be cleared.
	...
	}

	while (command != '5')
	{//Repeats the menu cycle until the quit command is given.
...
	}

	file.close();

	return 0;
}

//Function file
#include "hardware.h"
using namespace std;

bool fileCheck(char fileName[])
//Checks if the file exists.
{
...
}


void initialiseBinary(fstream &file)
//Initialises the binary file with 100 structs.
{
...
}


void partInput(fstream &outFile)
//Gets user input and outputs it to structs in binary file.
{
...
}


void partList(fstream &inFile)
//Displays all the stored records;
{
...
}


void updatePart(fstream &upFile)
//Updates the information of a part.
{
...
}


void insertPart(fstream &upFile)
//Inserts a new record to the database.
{
...
}


void deletePart(fstream &upFile)
//Deletes a part.
{
...
}


bool partSearch(fstream &inFile, int idNum)
//Searches for a part based on ID number, returns true if found.
{
...
}

void printMenu()
//Outputs command menu.
{
...
}


Thanks for any help.
Last edited on
In the header file you have forgot that fstream is in the std namespace.
I'm not sure I understand, I tried commenting out #include <fstream> and still got the same errors, as well as more from the library being missing.
Ok, I will clarify what I mean. fstream is in the std namespace so normally one would write std::fstream. The reason it works without std:: in your other files and in your original program is because you have put using namespace std; on top.
Last edited on
Ah I see, thank you. I've read elsewhere that it's considered bad style to put using namespace std; in the header file, so should I be putting the std::fstream in or just sticking namespace std in the header anyway?
Put std::fstream and if you want to avoid the possibility of very weird errors in the future then get rid of using namespace std from all files and just prefix std to everything. It's really not as time consuming as it seems once you get used to it.
Topic archived. No new replies allowed.