error C2678: binary '=' :

I know that this is home work and I don't want no one to take duty to solve the problem.I want to know where is the error.Here is my code and I am can't figure out what is wrong with the operator =.The error occurs in the "void CStudentGroup::makeRanges":

Error 2 no operator found which takes a left-hand operand of type 'const CStudent' (or there is no acceptable conversion)


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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
#include <fstream>
#include <iterator>

using namespace std;

class CStudent
{
	string m_strFN;
	int m_iPoints;
public:
	CStudent() {}
	CStudent (int iPoints) 
	{
		m_iPoints = iPoints;
	}
	CStudent(const CStudent& e)
	{
		m_strFN = e.m_strFN;
		m_iPoints = e.m_iPoints;
	}
	~CStudent() {}
	void Output (ostream& toStream) const 
	{
		toStream << "Student Fac# = " << GetFN() << " " << GetPoints() << endl;
	}
	void Input( istream& fromStream)
	{
		fromStream >> m_strFN >> m_iPoints;
	}
	void SetFN (const string &strFN)
	{
		m_strFN = strFN;
	}
	const string& GetFN() const 
	{
		return m_strFN;
	}
	void SetPoints (const int _iPoints)
	{
		m_iPoints = _iPoints;
	}
	const int GetPoints() const 
	{
		return m_iPoints;
	}
	friend ostream& operator<< (ostream& toStream, const CStudent& stud)
	{
		stud.Output(toStream);
		return toStream;
	}
	friend istream& operator>> (istream& fromStream,CStudent& stud)
	{
		stud.Input(fromStream);
		return fromStream;
	}
	CStudent& operator=(CStudent& st)
	{
		m_strFN = st.m_strFN;
		m_iPoints = st.m_iPoints;
		return *this;
	}
	bool operator< (const CStudent& e) const
	{
		return m_iPoints < e.m_iPoints;
	}
	bool operator== (const CStudent& e) const
	{
		return m_iPoints == e.m_iPoints;
	}
	static bool GetPortionGlobal (const CStudent& value)
	{
		g_iCurrent++;
		return (g_iCurrent < g_iRange);
	}
	static int g_iRange;
	static int g_iCurrent;
};

int CStudent::g_iRange = 0;
int CStudent::g_iCurrent = 0;

class CStudentGroup 
{
	set<CStudent> m_setStudents;
public:
	CStudentGroup(set<CStudent>& set)
	{
		m_setStudents = set;
	}
	CStudentGroup() {}
	void readIn(ifstream ifile);
	void printGroup(int iFrom,int iTo);
	void makeRanges(int& iP1,int& iP2,int& iP3,int& iP4);
};

void CStudentGroup::readIn(ifstream ifile)
{
	if (m_setStudents.size())
	{
		m_setStudents.clear();
	}
	copy (istream_iterator<CStudent> (ifile),istream_iterator<CStudent>(),inserter(m_setStudents,m_setStudents.begin()));
}

void CStudentGroup::printGroup(int iFrom,int iTo)
{
	set<CStudent>::iterator Strudentiter1 = lower_bound(m_setStudents.begin(),m_setStudents.end(),CStudent(iFrom));
	set<CStudent>::iterator Strudentiter2 = lower_bound(m_setStudents.begin(),m_setStudents.end(),CStudent(iTo));
	if (Strudentiter2 != m_setStudents.end())
	{
		Strudentiter2--;
	}
	copy(Strudentiter1,Strudentiter2,ostream_iterator<CStudent>(cout,"'\n"));
}

class GetPortion
{
public:
	GetPortion(int iInit) : m_iCurrent(0),m_iRange(iInit) {}
	bool operator() (CStudent value)
	{
		m_iCurrent++;
		return (m_iCurrent < m_iRange);

	}
private:
	int m_iCurrent,m_iRange;
};


void CStudentGroup::makeRanges(int& iP1,int& iP2,int& iP3,int& iP4)
{
	CStudent::g_iRange = m_setStudents.size() / 4;
	CStudent::g_iCurrent = 0;
	
	if (!CStudent::g_iRange)
	{
		iP1 = iP2 = iP3 = iP4 = 0;
		return;
	}
	set<CStudent>::iterator split = partition(m_setStudents.begin(),m_setStudents.end(),CStudent::GetPortionGlobal);
	iP1 = (*(++split)).GetPoints();
	CStudent::g_iCurrent = 0;
	split = partition(split,m_setStudents.end(),CStudent::GetPortionGlobal);
	iP2 = (*(++split)).GetPoints();
	CStudent::g_iCurrent = 0;
	split = partition(split,m_setStudents.end(),CStudent::GetPortionGlobal);
	iP3 = (*(++split)).GetPoints();
	CStudent::g_iCurrent = 0;
	split = m_setStudents.end();
	iP4 = (*(--split)).GetPoints();
}

int main()
{
	CStudent oSt1,oSt2,oSt3,oSt4;
	oSt1.SetFN("106031");
	oSt1.SetPoints(10);
	oSt2.SetFN("106032");
	oSt2.SetPoints(20);
	oSt3.SetFN("106033");
	oSt3.SetPoints(30);
	oSt4.SetFN("106034");
	oSt4.SetPoints(60);
	set<CStudent> student_set;
	student_set.insert(oSt1);
	student_set.insert(oSt2);
	student_set.insert(oSt3);
	student_set.insert(oSt4);
	CStudentGroup stGroup (student_set);
	CStudentGroup stGroup2;
	stGroup2.readIn(ifstream("input.txt"));
	cout << "*************\n";
	int iP1 = 0, iP2 = 0, iP3 = 0, iP4 = 0;
	stGroup2.makeRanges(iP1,iP2,iP3,iP4);
	cout << "GR_1: " << iP1 << endl;
	stGroup2.printGroup(0,iP1);
	cout << "GR_2: " << iP2 << endl;
	stGroup2.printGroup(iP1,iP2);
	cout << "GR_3: " << iP3 << endl;
	stGroup2.printGroup(iP2,iP3);
	cout << "GR_4: " << iP4 << endl;
	stGroup2.printGroup(iP3,iP4);
	return 0;
}
Last edited on
Static members of classes can't be set like regular data memebers. http://www.cplusplus.com/doc/tutorial/classes2/#static
no operator found which takes a left-hand operand of type 'const CStudent' (or there is no acceptable conversion)

std::partition doesn't work with std::set (line 145)

The error is telling you that you're not allowed to swap elements, as they're constant.

1
2
3
4
5
6
7
		// TEMPLATE FUNCTION iter_swap
template<class _FwdIt1,
	class _FwdIt2> inline
	void iter_swap(_FwdIt1 _Left, _FwdIt2 _Right)
	{	// swap *_Left and *_Right
	swap(*_Left, *_Right);
	}


This is because std::set looks after it's own ordering, according to the predicate it's provided with (the default is operator<). It doesn't want anyone else to reorder it!

1
2
3
4
5
6
7
8
9
		// TEMPLATE CLASS set
template<class _Kty,
	class _Pr = less<_Kty>,
	class _Alloc = allocator<_Kty> >
	class set
		: public _Tree<_Tset_traits<_Kty, _Pr, _Alloc, false> >
	{	// ordered red-black tree of key values, unique keys
public:
	// etc 


I'm not sure why you need to use std::partition, but if you if you want to use it you will have to swap the set for a vector, or create a temp vector and copy the contents of the set into that for partitioning.

Or maybe you can come up with a different approach?

Actually, why are you using set in the first place? Is it needed??

Andy

PS Misc...

- you should use the constructor initializer list more, including initializing all members
- use const wherever possible, but not where inappropriate
- and you could tighten up CStudentGroup::readIn to

void readIn(istream& fromStream);

as you aren't using file specific calls within the method.

Last edited on
Topic archived. No new replies allowed.