binary '<<': no operator found which takes a right-hand operand of type 'const studentinfo

when i run this code , i am getting

Severity Code Description Project File Line Suppression State
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const studentinfo' (or there is no acceptable conversion) studentrecord c:\users\admin\desktop\visualstudio\examreview\studentrecord\source.cpp 65

i thought i have taken care of it, because i have overloaded the << operator


ostream& operator <<(ostream& os, studentinfo & c)
{
os << c.name << " " << c.grade ;
return os;

}





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
 
# include <iostream>
# include <algorithm>
# include <vector>
# include <list>
# include <string>
# include <set>


using namespace std;

struct studentinfo {
	string name;
	int grade;

	bool operator<(const studentinfo& other) const { return name < other.name; }

};


class markinfo {

public:

	vector<studentinfo> pass;
	vector <studentinfo> fail;

};


markinfo passfail( const vector<studentinfo>  & s)
{
	markinfo m;

	for(auto x:s)

	if (x.grade >= 50)
	{
		m.pass.push_back(x); 
	}
	else
	{
		m.fail.push_back(x);
	}

	return m; 

}


ostream& operator <<(ostream& os, studentinfo & c)
{
	os << c.name << " " << c.grade ;
	return os;

}



template<typename T>
void print(T& c)
{
	cout << "\nHere is the List: \n";
	for (auto& x : c)			//& pass by ref is required if process modifies the element
		cout << x << "  ";
	cout << "\n" << endl;
}




bool cmp_names(const studentinfo & lhs, const studentinfo & rhs)
{
	return lhs.name < rhs.name; 
}




int main()
{

	vector <studentinfo> s;

	studentinfo sinfo;
	sinfo.name = "gg";
	sinfo.grade = 44;

	studentinfo sinfo2;
	sinfo2.name = "aa";
	sinfo2.grade = 77;

	s.push_back(sinfo);
	s.push_back(sinfo2);

	sort(s.begin(), s.end(), cmp_names);

	markinfo m;

	m = passfail(s);
	cout << " test " << endl; 


	list <studentinfo> l;

	std::list<studentinfo> lst{ s.begin(), s.end() };

	set<studentinfo> ss(s.begin(), s.end()); 

	print(lst); 

	print(ss); 


	for (auto& x : s)			//& pass by ref is required if process modifies the element
		cout << x << endl;


	
	//l.insert((s.begin(), s.end()));


	//ist <studentinfo> ll = ;

	/*
	for (auto x : m.pass)
	{
		cout << x.name << x.grade << endl;
	}*/

	
	for (auto x : s)
	{
		cout << x.name << x.grade << endl;
	}
	
	system("pause");
	

}
Use reference to const.

 
ostream& operator <<(ostream& os, const studentinfo& c)

Last edited on
why do i need to pass it as a const in:

ostream& operator <<(ostream& os, const studentinfo& c
why do i need to pass it as a const

Because the compiler says:
In instantiation of 'void print(T&) [with T = std::set<studentinfo>]':
112:10: required from here 
65:8: error: no match for 'operator<<' (operand types are 
             'std::ostream {aka std::basic_ostream<char>}' and 'const studentinfo')

Your code at line 112 states:
print(ss);
which causes a call to:
1
2
3
4
5
6
7
8
template<typename T>
void print(T& c)
{
	cout << "\nHere is the List: \n";
	for (auto& x : c)element
		cout << x << "  ";
	cout << "\n" << endl;
}

Which is a “range-based for loop”
http://en.cppreference.com/w/cpp/language/range-for
and so is turned into:
1
2
3
4
5
6
7
8
9
10
11
12
13
{

    auto && __range = range_expression ;
    auto __begin = begin_expr ;
    auto __end = end_expr ;
    for ( ; __begin != __end; ++__begin) {

        range_declaration = *__begin;
        loop_statement

    }

} 

Since std::set is an associative container where the value type is the same as the key type,
http://en.cppreference.com/w/cpp/container/set
because of the following requirements from the standard:
C++14 final_draft (n4140) - 23.2.4.6:
For associative containers where the value type is the same as the key type, both iterator and const_iterator are constant iterators.

the requested std::set::iterator, is declared const.

(Errors and Omissions Excepted)
Topic archived. No new replies allowed.