Header problems

Hi, so here is the thing. I had to create Document class that works like a simple text editor. So I created

document.h
1
2
3
4
5
6
7
8
#pragma once
#include "line.h"
#include <list>

class Document{
std::list<Line> lines;
// ...
};


Now when I include "document.h" I kind of #include "line.h" as well.
I really want to include just Document class without stuff like list and Line

I tried to do it like this
document.h
1
2
3
#pragma once

extern class Document;

document_impl.h
1
2
3
4
5
6
7
8
#include "line.h"
#include <list>
#include "document.h"

class Document{
std::list<Line> lines;
// ...
};

document.cpp
1
2
#include "document_impl.h
// ..." 


but that was not working because
incomplete type is not allowed

Is there a way I can only #include this Document class with its interface, without all the stuff thats being used to implement it?
Last edited on
Is there a way I can only #include this Document class with its interface, without all the stuff thats being used to implement it?

EDIT.

You only need to declare the class and then create and object. You should not need to include headers that the class was implemented with. Can we see more code? So we can reproduce your issue ?
Last edited on
actions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <string>

enum class action : char {
	insert = 'i', del = 'd'
};

class full_action {
	action act;
	std::string str;
	int line_nr;

public:
	full_action(action a, const std::string& s, int n) 
		: act{ a }, str{ s }, line_nr{ n } {}

	action get_action() const { return act; }
	std::string get_text() const { return str; }
	int get_num() const { return line_nr; }

};

line.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once

#include <string>
#include <iostream>

class Line {
	std::string line;

public:
	Line(const std::string& str) : line{ str } {}

	friend std::ostream& operator<<(std::ostream& os, const Line& l);
};

line.cpp
1
2
3
4
5
#include "line.h"

std::ostream& operator<<(std::ostream& os, const Line& l){
	return os << l.line;
}


document.h
1
2
#pragma once
extern class Document;


document_impl.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
#pragma once

#include <list>
#include "Line.h"
#include <vector>
#include "actions.h"
#include "document.h"

/*
‘‘Insert a string as line number n,’’
‘‘delete line n,’’ and
‘‘undo the last (non-undo) operation.’’
*/


class Document {
	std::list<Line> lines;
	mutable std::list<Line> current_update;
	std::vector<full_action> actions;

	mutable int num_of_lines_current = 0;
	mutable int num_of_lines = 0;
	mutable int actions_updated = 0;

	void update_current(const full_action&)const;
	void add_line_update_current(const full_action&)const;
	void del_line_update_current(const full_action&)const;
	void update_line_count(const std::vector<full_action>&, int&, int&);
public:

	Document() = default;
	Document(const Document& doc) :
		lines{ doc.lines }, current_update{ doc.current_update },
		actions{ doc.actions }, num_of_lines{ doc.num_of_lines },
		actions_updated{ doc.actions_updated },
		num_of_lines_current{ doc.num_of_lines_current } {}

	void add_line(const std::string& str, int line_nr);
	void del_line(int line_nr);
	void push_back(const std::string& str);
	void undo();

	void print()const;
};

document.cpp
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
#include "document_impl.h"

#include <iostream>
#include <string>
#include <stdexcept>


using namespace std;


//update current document
void Document::add_line_update_current(const full_action& act)const {
	
	if (act.get_num() == num_of_lines_current) {
		current_update.push_back(act.get_text());
		++num_of_lines_current;
		return;
	}
	auto it = current_update.begin();

	//update iterator so it points to right position
	for (int i = 0; i < act.get_num(); ++i) {
		it++;
	}
	
	//insert before
	current_update.insert(it, act.get_text());
	++num_of_lines_current;
}

void Document::del_line_update_current(const full_action& act)const {

	//ja rinda nr 0 tad push front jauno tekstu
	auto it = current_update.begin();

	//update iterator so it points to right position
	for (int i = 0; i < act.get_num(); ++i) {
		++it;
	}

	current_update.erase(it);
	--num_of_lines_current;
}

void Document::update_current(const full_action& act)const  {

	switch (act.get_action()) {
	case action::insert: {
		add_line_update_current(act);
		break;
	}
	case action::del: {
		del_line_update_current(act);
		break;
	}
	}
}




void Document::add_line(const std::string& str, int line_nr) {

	try {
		if (line_nr < 0) throw std::runtime_error{ "negative line number" };
		else if (line_nr >= num_of_lines) {

			for (int i = num_of_lines; i < line_nr; ++i, ++num_of_lines) {
				//add empty lines
				actions.push_back({ action::insert, "", i });
			}

			actions.push_back({ action::insert, str, line_nr });
			++num_of_lines;
		}
		else {
			actions.push_back({ action::insert, str, line_nr });
			++num_of_lines;
		}
	}
	catch (const std::runtime_error& err) {
		std::cout << "error : " << err.what() << std::endl;
	}
}

void Document::push_back(const std::string& str) {
	add_line(str, num_of_lines);
}

void Document::del_line(int line_nr) {

	try {
		if (line_nr < 0) throw std::runtime_error{ "negative line asked to delete" };
		else if (!num_of_lines || ++line_nr > num_of_lines)
			throw std::runtime_error{ "cant delete line that doesnt exist" };
		else {
			actions.push_back({ action::del, "", --line_nr });
			--num_of_lines;
		}
	}
	catch (const std::runtime_error& err) {
		std::cout << "error : " << err.what() << std::endl;
	}


}

void Document::print() const {
	//find a way to know how many actions are updated

	if (actions_updated != actions.size()) {

		//make updates
		for (int i = actions_updated; i < actions.size(); ++i) {
			update_current(actions[i]);
		}
		actions_updated = actions.size();
	}


	int index{0};
	for (auto& l : current_update) {
		std::cout << "[ " << index++ << " ]\t" << l << std::endl;
	}


}

void Document::update_line_count(const std::vector<full_action>& vec, 
	int& lines_current, int& lines) {

	//update theoretical lines
	switch (vec[vec.size() - 1].get_action()) {
	case action::insert: {--lines;  break; }
	case action::del: {++lines;  break; }
	}

	for (int i = 0; i < vec.size(); ++i) {
		switch (vec[i].get_action()) {
		case action::insert: {
			if(i < actions_updated)		//check if current lines update happened
				--lines_current;
			break;
		}
		case action::del: {
			if (i < actions_updated)    //check if current lines update happened
				++lines_current;
			break;
		}
		}
	}
}

void Document::undo() {

	if (actions.empty()) throw std::runtime_error{ "undo cant be made" };


	//update current lines and theoretical lines
	update_line_count(actions, num_of_lines_current, num_of_lines);

	//delete last action

	actions.pop_back();

	//have to create print from scratch
	current_update = lines;
	actions_updated = 0;
}


Thats basically it. I want to include document.h so other stuff like Line wouldn't be included

EDIT : damn I must work on my code so much. Looks so ugly and probably not efficient as well

I kind of tryed to declare the class so I dont have to show implementation yet, but the compiler says that I cant use it because its incomplete. If I include document_impl.h everything works fine
Last edited on
Topic archived. No new replies allowed.