>> and << for User-defined-type

Good day,

I'm having bit of an issue regarding writing to and printing from a user defined type.

Here is the code that doesn't work.

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
#include "stdafx.h"
#include "std_lib_facilities.h"

class name_value
{
public:
	string name;
	double score;
	name_value()
		:name(), score(0) {}
	name_value(string name, double score)
		:name(name), score(score) {}
};

int main()
{
	cout << "Please enter a name and score\n";
	cout << "Be sure to put a space between the name and score\n";

	name_value name_score;

	cout << ">: ";
	cin >> name_score;
	cout << '\n' << name_score << '\n';

	keep_window_open();
	return 0;
}


Now here is the code that does.

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
#include "stdafx.h"
#include "std_lib_facilities.h"

class name_value
{
public:
	string name;
	double score;
	name_value()
		:name(), score(0) {}
	name_value(string name, double score)
		:name(name), score(score) {}
};

int main()
{
	cout << "Please enter a name and score\n";
	cout << "Be sure to put a space between the name and score\n";

	name_value name_score;

	cout << ">: ";
	cin >> name_score.name >> name_score.score;
	cout << '\n' << name_score.name << " " << name_score.score << '\n';

	keep_window_open();
	return 0;
}


The issue is that this is just the first step in building a bit more complex program.

Using the class "name_value", I'll need to make a vector with it.

vector<name_value>name_score_vector;

I then need to be able to print out each name_score within the vector on separate lines. Before that, I need to compare name_score to what already in the vector to make sure the same name is not repeated twice.

The error I get is;

E0349 no operator ">>" matches these operands
C2679 binary '>>': no operator found which takes a right-hand operand of type 'name_value' (or there is no acceptable conversion)

Is there something I can add to the type definition so ">>" and "<<" are recognized simply using "name_class" and not "name_class.name" or "name_class.value"?

Thank you.
The search term is "operator overload".

https://msdn.microsoft.com/en-us/library/x6aebccc.aspx
Thank for the reply. This looks like what I'm looking for but I'm afraid I don't quite understand what is happening wrt the following;

1
2
3
4
5
istream& operator>> (istream& is, Date& dt)  
{  
    is>> dt.mo>> dt.da>> dt.yr;  
    return is;  
}


Edit: Figured it out.

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
#include "stdafx.h"
#include "std_lib_facilities.h"

class name_value
{
public:
	string name;
	double score;
	name_value()
		:name(), score(0) {}
	name_value(string name, double score)
		:name(name), score(score) {}
};

ostream& operator<<(ostream& os, name_value& name_score)
{
	os << name_score.name << " " << name_score.score;
	return os;
}

istream& operator>>(istream& is, name_value& name_score)
{
	is >> name_score.name >> name_score.score;
	return is;
}

vector<name_value> name_score_vector;

name_value check_name_score(name_value name_score)
{
	if (name_score_vector.size() == 0)
		return name_score;
	else
		for (int i = 0; i < name_score_vector.size(); i++)
		{
			if (name_score.name == name_score_vector[i].name)
				error("Same name entered");
			else
				return name_score;
		}
}

int main()
try
{
	cout << "Please enter a name and score\n";
	cout << "Be sure to put a space between the name and score\n";
	cout << "enter ';' for the name and score to terminate input and get result\n";

	name_value name_score;
	while (name_score.name != ";")
	{
		cout << ">: ";
		cin >> name_score;
		check_name_score(name_score);
		name_score_vector.push_back(name_score);
	}
	cout << '\n';
	for (int i = 1; i < name_score_vector.size(); i++)
		cout << name_score_vector[i-1] << '\n';

	keep_window_open("~0");
	return 0;
}
catch (exception& e)
{
	cerr << e.what() << '\n';
	keep_window_open("~1");
	return 1;
}
catch (...)
{
	cerr << "Unknown Exception\n";
	keep_window_open("~2");
	return 2;
}


Good enough to call the exercise done.
Last edited on
Topic archived. No new replies allowed.