Data Accessors

Hello. I am new to C++ coding, and am learning data accessors.
This code will not compile for me and I cannot figure out why.

Thank you for your help.

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
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <string>

using namespace std;

class Printer 
{
	
private:

	string model;
	string manufacturer;
	int ink;
	int pages;

public:
		
	Printer (string, string, int, int);

         void print();
	void error();
	int loadPages (int);
	int loadInk (int);
	
	int getPages();
	int getInk();
	string getInfo();
};

Printer::Printer(string m, string f, int i, int p)	
{
	model = m;
	manufacturer = f;
	ink = i;
	pages = p;
}

int main()
{
	Printer myPrinter ("42","Toshiba",100,100);		

	myPrinter.print();						
	
	string info = myPrinter.getInfo();
	cout << info;

	myPrinter.loadPages(100);
	cout << &Printer::getPages;

	cin.get();
	cin.ignore();
	return 0;
}


void Printer::print()		
	{
	if (pages < 1)
	{
		error();	
	}
	else
	{
		pages--;	
		ink--;		
	}
}
void error()
{
	cout << "Out of pages. Please insert more.";
}
int Printer::loadPages (int p)
{
	pages = pages + p;
	return pages;
}
int Printer::loadInk (int i)
{
	ink = ink + i;
	return ink;
}


int Printer::getPages()		
{
	return pages;
}
int Printer::getInk()		
{
	return ink;
}
string Printer::getInfo()	
{
	return model + " " + manufacturer + " " + ink + " " + pages;
};
Last edited on
1
2
return model + " " + manufacturer + " " + ink + " " + pages;
                                         ^^^^        ^^^^^


string::operator+ can only take strings for it's arguments.

you must convert ink and pages to string first.

U must enable C++11 in ur compiler and do :

return model + " " + manufacturer + to_string(ink) + " " + to_string(pages);

You also have an extra semicolon @line 97.

You also forgot to add Printer:: in ur definition of error()
Last edited on
Why is this error popping up?

error LNK2019: unresolved external symbol "public: void __thiscall Printer::error(void)" (?error@Printer@@QAEXXZ) referenced in function "public: void __thiscall Printer::print(void)" (?print@Printer@@QAEXXZ)
see my updated comment
Ok!

It is outputting

"42 Toshiba" as expected.. but it is also outputting "99 991" for ink and pages. Why is that?

Thank you nvrmnd for your help by the way.
you forgot to add a newline after you cout << info()

cout << &Printer::getPages;
Do you mean myPrinter.getPages() instead ?
Hmm, with this as my new int main, it is outputting "42 Toshiba 99 99199" now.. Why? haha

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
	Printer myPrinter ("42","Toshiba",100,100);

	myPrinter.print();							
	
	string info = myPrinter.getInfo();
	cout << info;

	myPrinter.loadPages(100);
	cout << myPrinter.getPages() ;

	cin.get();
	cin.ignore();
	return 0;
}
Last edited on
Here is the whole code if that makes it easier.

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
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <string>

using namespace std;

class Printer 
{
	
private:

	string model;
	string manufacturer;
	int ink;
	int pages;

public:
		
	Printer (string, string, int, int);

	
	void print();
	void error();
	int loadPages (int);
	int loadInk (int);
	
	int getPages();
	int getInk();
	string getInfo();
};

Printer::Printer(string m, string f, int i, int p)	
{
	model = m;
	manufacturer = f;
	ink = i;
	pages = p;
}

int main()
{
	Printer myPrinter ("42","Toshiba",100,100);		

	myPrinter.print();								
	
	string info = myPrinter.getInfo();
	cout << info;

	myPrinter.loadPages(100);
	cout << myPrinter.getPages() ;

	cin.get();
	cin.ignore();
	return 0;
}


void Printer::print()		
{
	if (pages < 1)
	{
		error();	
	}
	else
	{
		pages--;	
		ink--;		
	}
}
void Printer::error()
{
	cout << "Out of pages. Please insert more.";
}
int Printer::loadPages (int p)
{
	pages = pages + p;
	return pages;
}
int Printer::loadInk (int i)
{
	ink = ink + i;
	return ink;
}

int Printer::getPages()	
{
	return pages;
}
int Printer::getInk()		
{
	return ink;
}
string Printer::getInfo()
{
	return model + " " + manufacturer + " " + to_string(ink) + " " + to_string(pages);
}
Why is this outputting "42 Toshiba 99 99199"?
you forgot to add a newline after you cout << info()

1
2
42 Toshiba 99 99199
                ^


http://www.cplusplus.com/forum/beginner/130102/#msg702370
Last edited on
Oh, wow. I totally skipped over you telling me that before. Thank you so much!
Topic archived. No new replies allowed.