Headers

Hi guys! I want to use two header files in my program. Here is exactly what I want to do.

-In the first header I have a binary tree and a structure.
-In the second file I have another functions that need to use the structure in the first header.
-I also want to use a function from the second header in the first.
-And finally I want to do actions with both headers in a "main.cpp" file that contains only int main() function.

Can you tell me how to include the headers in each other and in the main.cpp to be able to do the actions above?

I try to include the first header in the second one and the second one in the first header. Then I include both headers in the main.cpp file. But the compiler shows me many errors.

I hope I was clear in my explanation.
Probably both headers need each other.
You probably need to use multiple .cpp files.
If you show more infos, we may help you in a better way.
> I also want to use a function from the second header in the first.
You shouldn't write function definitions in a header (unless they are inline or template)

Everything must be declared before its first use
By instance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//a.h
#ifndef A_H
#define A_H

struct node{
   void bar();
};

#endif

//b.h
#include "a.h"
void foo(node);

//a.cpp
#include "a.h"
#include "b.h"
void node::bar(){
   foo(*this);
}


http://www.cplusplus.com/forum/articles/10627/ (especially point 4)
Last edited on
Are you using header guards?? (aka include guards)

This is prob. a good part of the problem if you're talking about redefinitition errors.

Andy

Include guards
http://www.cplusplus.com/forum/general/71787/

How to use header guards?
http://www.cplusplus.com/forum/beginner/110161/

One Definition Rule
http://en.wikipedia.org/wiki/One_Definition_Rule
I placed the structure in the second file so now only the first file is dependent to the second. In my main.cpp file I first include the second and then the first header. Have a look at my code and please tell me if you have advices how to make it work or look better. :) The whole program is about reading data about books from two text files and writing this data in a binary file. Also there is option to output the binary file. Then I put the data from the binary file in Ordered Binary Tree and output it in an alphabetical order (I compare the data.author info).

CreateBinary.h
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
#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

//The structure that I need in both headers
struct Data
	{
		char sign[10]; //aut.txt and info.txt
		char name[20]; //aut.txt
		char author[20]; //info.txt
		int pages; //info.txt
		int year; //info.txt
	};

void InsertData()
	{
		Data data;
		cout << "Sign  : ";
		cin >> data.sign;
		cout << "Name  : ";
		cin.get();
		cin.getline(data.name, 20);
		cout << "Author: ";
		cin.getline(data.author, 20);
		cout << "Pages : ";
		cin >> data.pages;
		cout << "Year  : ";
		cin >> data.year;

		ofstream aut("aut.txt", ios::app);
		aut << data.sign << endl;
		aut << data.name << endl;
		aut.close();

		ofstream info("info.txt", ios::app);
		info << data.sign << endl << data.author << endl << data.pages << endl << data.year << endl;
		info.close();
	}

void Create_Binary()
	{
		ifstream aut("aut.txt");
		ifstream info("info.txt");
		ofstream Binary("Bibl.bin", ios::out|ios::binary);
		Data data;

		while(aut.good())
			{
				aut.getline(data.sign, 20, '\n');
				aut.getline(data.name, 20, '\n');
				aut.get();

				info >> data.sign;
				info.get();
				info.getline(data.author, 20, '\n');
				info >> data.pages;
				info.get();
				info >> data.year;
				info.get();

				Binary.write((const char*)&data, sizeof(Data));
			}
		aut.close();
		info.close();
		Binary.close();
	}

void Table_Comp()
	{
		cout << endl;
		cout << setw(10) << setiosflags(ios::left) << "Sign" << setw(20) << "Name" << setw(20) << "Author"
			<< setw(8) << "Pages" << setw(5) << resetiosflags(ios::left) << "Year" << endl;
	}

void ShowBook(const Data &data)
	{
		cout << setw(10) << setiosflags(ios::left) << data.sign << setw(20) << data.name << setw(20) << data.author
			<< setw(8) << data.pages << setw(5) << resetiosflags(ios::left) << data.year << endl;
	}

void ShowBin()
	{
		Data data;
		Table_Comp();
		ifstream Binary("Bibl.bin", ios::in|ios::binary);
		while(Binary.good())
			{
				Binary.read((char*)&data, sizeof(Data));
				if(Binary.good()) ShowBook(data);
			}
		cout << endl;
		Binary.close();
	}


Tree.h
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
#include<iostream>
#include<fstream>
using namespace std;

struct Node
	{
		Data data;
		Node *Left;
		Node *Right;
	};

class Tree
	{
	private:
		Node *root;
	public:
		Tree();
		void Insert(Node *&, Data);
		void CreateTree();
		void BinaryFile(Node *node); //I haven't wrote this function yet but it will put the ordered data in a new binary file
		void Show(Node *&node);
	};

Tree::Tree()
	{
		root = NULL;
	}

void Tree::Insert(Node *&node, Data data)
	{
		if(root == NULL)
			{
				root = new Node;
				root->data = data;
				root->Left = NULL;
				root->Right = NULL;
			}
		else
			if(strcmp(node->data.author, data.author) == 1)
				{
					if(node->Left != NULL) Insert(node->Left, data);
					else
						{
							node->Left = new Node;
							node->Left->data = data;
							node->Left->Left = NULL;
							node->Left->Right = NULL;
						}
				}
			else
				{
					if(node->Right != NULL) Insert(node->Right, data);
					else
						{
							node->Right = new Node;
							node->Right->data = data;
							node->Right->Left = NULL;
							node->Right->Right = NULL;
						}
				}
	}

void Tree::CreateTree()
	{
		ifstream file("Bibl.bin", ios::in|ios::binary);
		Data book;
		while(file.good())
			{
				file.read((char*)&book, sizeof(Data));
				if(file.good()) Insert(root, book);
			}
		file.close();
		Show(root);
	}

void Tree::Show(Node *&node)
	{
		if(node == NULL) return;
		Show(node->Left);
		ShowBook(node->data); //The function that is in the other header
		Show(node->Right);
	}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include"CreateBinary.h" //The structure is in this header and it is independent
#include"Tree.h" //I have already declared the structure and functions int CreateBinary.h
using namespace std;



int main()
	{
		InsertData();
		Create_Binary();
		ShowBin();

		Tree A;
		A.CreateTree();

		system("pause");
		return 0;
	}
Last edited on
Thanks @andywestken, I will look more careful to the header guards :)
Last edited on
Topic archived. No new replies allowed.