Error in map

Hello,

could you please help me?

Severity Code Description Project File Line Suppression State
Error C2676 binary '<': 'const _Ty' does not define this operator or a conversion to a type acceptable to the predefined operator C++_6 C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.28.29910\include\xstddef 127


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
  #include <iostream>
#include <vector>
#include <iterator>
#include <map>
using namespace std;

class Student
{
private:
	int SID;
	string name;
	string family;
	double mean;
public:
	Student(int id = 0, string _name = " ", string _family = " ", double _mean = 0)
	{
		SID = id;
		name = _name;
		family = _family;
		mean = _mean;
	}
	Student(const Student& s)
	{
		SID = s.SID;
		name = s.name;
		family = s.family;
		mean = s.mean;
	}
	void setSID(int id)
	{
		SID = id;
	}
	void setName(string _name)
	{
		name = _name;
	}
	void setFamily(string _family)
	{
		family = _family;
	}
	void setMean(double _mean)
	{
		mean = _mean;
	}
	int getSID()
	{
		return SID;
	}
	string getName()
	{
		return name;
	}
	string getFamily()
	{
		return family;
	}
	double getMean()
	{
		return mean;
	}

};

class Course
{
	int CID;
	string name;
	string profName;
	int numOfTAs;
	int numOfStudents;
	vector<Student> TAs;
	map<Student, int> studentGrades;
public:
	Course(vector<Student> _TAs, map<Student, int>  _studentGrades,
		int id = 0, string _name = "", string _profName = "",
		int _numOfTAs = 0, int _numOfStudents = 0)
	{
		CID = id;
		name = _name;
		profName = _profName;
		numOfTAs = _numOfTAs;
		numOfStudents = _numOfStudents;
		TAs = _TAs;
		studentGrades = _studentGrades;
	}
};

int main()
{
	//Student s1(10, "Dani", "Babak", 10);
	//Student s2(11, "Iran", "Irani", 10);
	//Student s3(12, "Ebi", "mahdi", 10);

	//vector<Student> Students;
	//Students.push_back(s1);
	//Students.push_back(s2);
	//Students.push_back(s3);

	map<Student, int> myStudentsGrades;

	Student s4(10, "TA04", "TA4", 4);
	Student s5(11, "TA05", "TA5", 5);
	Student s6(12, "TA06", "TA6", 6);

	//myStudentsGrades[s4] = 20;

	myStudentsGrades.insert(make_pair(s4, 20)); // ERROR
	//studentsGrades.insert(pair<Student, int>(s5, 19));
	//studentsGrades.insert(pair<Student, int>(s6, 18));
}

If you use a Student as a key for a std::map, then you need to define an ordering relation (i.e. the operator < ) for a Student.
I solved the previous error but now I have another error:

Severity Code Description Project File Line Suppression State
Error (active) E0289 no instance of constructor "Course::Course" matches the argument list C++_6 C:\Users\My\source\repos\C++_6\C++_6\Source.cpp 134


Severity Code Description Project File Line Suppression State
Error C2664 'Course::Course(std::vector<Student,std::allocator<Student>>,std::map<Student,int,std::less<Student>,std::allocator<std::pair<const Student,int>>>,int,std::string,std::string,int,int)': cannot convert argument 1 from 'std::vector<Student,std::allocator<Student>> *' to 'std::vector<Student,std::allocator<Student>>' C++_6 C:\Users\My\source\repos\C++_6\C++_6\Source.cpp 122



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
#include <iostream>
#include <vector>
#include <iterator>
#include <map>
using namespace std;

class Student
{
private:
	int SID;
	string name;
	string family;
	double mean;
public:
	Student(int id = 0, string _name = " ", string _family = " ", double _mean = 0)
	{
		SID = id;
		name = _name;
		family = _family;
		mean = _mean;
	}
	Student(const Student& s)
	{
		SID = s.SID;
		name = s.name;
		family = s.family;
		mean = s.mean;
	}
	void setSID(int id)
	{
		SID = id;
	}
	void setName(string _name)
	{
		name = _name;
	}
	void setFamily(string _family)
	{
		family = _family;
	}
	void setMean(double _mean)
	{
		mean = _mean;
	}
	int getSID()
	{
		return SID;
	}
	string getName()
	{
		return name;
	}
	string getFamily()
	{
		return family;
	}
	double getMean()
	{
		return mean;
	}

};

class Course
{
	int CID;
	string name;
	string profName;
	int numOfTAs;
	int numOfStudents;
	vector<Student> TAs;
	map<Student, int> studentGrades;
public:
	Course(vector<Student> _TAs, map<Student, int>  _studentGrades,
		int id = 0, string _name = "", string _profName = "",
		int _numOfTAs = 0, int _numOfStudents = 0)
	{
		CID = id;
		name = _name;
		profName = _profName;
		numOfTAs = _numOfTAs;
		numOfStudents = _numOfStudents;
		TAs = _TAs;
		studentGrades = _studentGrades;
	}
	bool addTA(Student ta)
	{
		vector<Student>::iterator it;
		it = find(TAs.begin(), TAs.end(),ta);
		if (it != TAs.end())
		{
			TAs.push_back(ta);
			return true;
		}
		else
			return false;
	}
	bool addStudent(Student student)
	{
		map<Student, int>::iterator it;
		it = find(studentGrades.begin(), studentGrades.end(), student);
		if (it != studentGrades.end())
		{
			studentGrades.insert(make_pair(student, 0));
			return true;
		}
		else
			return false;
	}
};

int main()
{
	Student s1(10, "Dani", "Babak", 10);
	Student s2(11, "Iran", "Irani", 10);
	Student s3(12, "Ebi", "mahdi", 10);

	vector<Student> Students ;
	Students.push_back(s1);
	Students.push_back(s2);
	Students.push_back(s3);

	map<Student*, int> myStudentsGrades;

	Student* s4 = new Student(10, "TA04", "TA4", 4);
	Student* s5 = new Student(11, "TA05", "TA5", 5);
	Student* s6  = new Student(12, "TA06", "TA6", 6);


	myStudentsGrades.insert(make_pair(s4, 20)); 
	myStudentsGrades.insert(make_pair(s5, 19));
	myStudentsGrades.insert(make_pair(s6, 18));

	Course Math(Students, myStudentsGrades, 101, "Math", "mahdi", 3, 3); //ERROR
}

Last edited on
Topic archived. No new replies allowed.