Program from C++ Primer not compiling?

I really have no idea what the problem is as Visual Studio doesn't find a problem with any of the code. I've been trying to read along and get the program that they're using to run but it's not building. What's causing this?

* EDIT: Figured it out. The error wasn't detected by VS, but it was due to the same constructor definition being used - also incorrectly, at that.

Screen.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
#ifndef SCREEN_H
#define SCREEN_H

#include <iostream>
#include <string>
#include <vector>
using std::string;

class Screen
{
public:
	typedef string::size_type pos;
private:
	pos cursor = 0;
	pos height = 0;
	pos width = 0;
	string contents = "";

	void do_display(std::ostream &os) const { os << contents; }
public:
	Screen() = default;
	Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {}
	Screen(pos ht, pos wd, char s) : height(ht), width(wd) { char s = ' '; contents(ht * wd, s) }
	char get() const { return contents[cursor]; }
	inline char get(pos ht, pos wd) const;
	Screen &move(pos r, pos c);
	Screen &set(char);
	Screen &set(pos, pos, char);
	Screen &display(std::ostream &os) { do_display(os); return *this; }
	const Screen &display(std::ostream &os) const { do_display(os); return *this; }
};

#endif

inline Screen &Screen::move(pos r, pos c)
{
	pos row = r * width;
	cursor = row + c;
	return *this;
}

char Screen::get(pos r, pos c) const
{
	pos row = r * width;
	return contents[row + c];
}

inline Screen &Screen::set(char c)
{
	contents[cursor] = c;
	return *this;
}

inline Screen &Screen::set(pos r, pos c, char ch)
{
	contents[r * width + c] = ch;
}


Source.cpp
1
2
3
4
5
6
7
8
int main()
{
	Screen myScreen(5, 5, 'X');
	myScreen.move(4, 0).set('#').display(std::cout);
	std::cout << std::endl;
	myScreen.display(std::cout);
	std::cout << std::endl;
}
Last edited on
Topic archived. No new replies allowed.