I made a small console program which has a class called Class_Room. I can make objects of school classrooms from it and store student names, roll numbers and ages in it. The student data are stored in a map. I wanted to make classroom object like this:
Class_Room class8C = {
{"student x", 2}, //2 for rollnumber
{"student y", 3}
};
So i made a conversion constructor:
Class_Room(std::map<int, std::string> map) {
for (std::pair<int, std::string> value : map) {
student_data[value.first] = { value.second, "NIL" };
}
}
but i get errors: no instance of constructor Class_Room::Class_Room matches argument list
"initializing": cannot convert "initializer list" to Class_Room
Here is my whole code:
#include <iostream>
#include <vector>
#include <string>
#include <map>
class Class_Room {
public:
Class_Room(std::map<int, std::string> map) {
for (std::pair<int, std::string> value : map) {
student_data[value.first] = { value.second, "NIL" };
}
}
void new_student(int roll, std::string name, int age = 0)
{
data.clear();
data.push_back(name);
if (age != 0) {
data.push_back(std::to_string(age));
}
else {
data.push_back("NIL");
}
student_data[roll] = data;
}
std::map<int, std::vector<std::string>> get_student_data_map()
{
return student_data;
}
private:
std::map<int, std::vector<std::string>> student_data;
std::vector<std::string> data;
};
std::ostream& operator<< (std::ostream& output, Class_Room classroom)
{
std::map<int, std::vector<std::string>> student_data = classroom.get_student_data_map();
for (std::pair<int, std::vector<std::string>> value : student_data) {
output << value.first << " => " << value.second[0] << ", " << value.second[1] << "\n";
}
return output;
}
int main() {
Class_Room class8C = {{"Ihsan", 2}};
std::cout << class8C;
}
I'm a beginner to c++ and don't know what went wrong, any help would be appreciated.