C++ 向映射添加类
我正在尝试将一个类对象添加到一个映射,这是我所拥有的:C++ 向映射添加类,c++,class,map,C++,Class,Map,我正在尝试将一个类对象添加到一个映射,这是我所拥有的: #include<vector> #include<map> #include<stdio.h> #include<string> #include<iostream> using namespace std; class Student{ int PID; string name; int academicYear; public: Stude
#include<vector>
#include<map>
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
class Student{
int PID;
string name;
int academicYear;
public:
Student(int, string, int);
};
Student::Student (int P, string n, int a) {
PID = P;
name = n;
academicYear = a;
}
void createStudent(map<string, Student>);
int main(int argc, char** argv){
map <string, Student> studentList;
createStudent(studentList);
}
void createStudent(map<string, Student> studentList){
int PID;
string name;
int academicYear;
cout << "Add new student-\nName: ";
getline(cin, name);
cout << "PID: ";
cin >> PID;
cout << "Academic year: ";
cin >> academicYear;
Student newstud (PID, name, academicYear);
studentList[name] = newstud; //this line causes the error:
//no matching function for call to
//'Student::Student()'
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
班级学生{
int-PID;
字符串名;
国际学术年;
公众:
学生(int,string,int);
};
学生::学生(整数P,字符串n,整数a){
PID=P;
name=n;
学年=a;
}
学生(地图);
int main(int argc,字符**argv){
地图学生名单;
创建学生(学生列表);
}
void createStudent(映射studentList){
int-PID;
字符串名;
国际学术年;
cout-PID;
cout>学年;
学生新闻(PID、姓名、学年);
studentList[name]=newstud;//此行导致错误:
//没有用于调用的匹配函数
//'Student::Student()'
}
我不明白为什么在那里调用构造函数,我想newstud应该已经从前一行构造出来了。有人能解释一下我尝试将newstud添加到地图时发生了什么吗?为了使用该函数添加条目,您应该让您的参数
studentList
通过引用传递
而不是
studentList[name] = newstud;
使用:
不过,这只是一个建议。为了使用函数添加条目,您应该让参数
studentList
通过引用传递
而不是
studentList[name] = newstud;
使用:
不过,这只是一个建议。为了使用函数添加条目,您应该让参数
studentList
通过引用传递
而不是
studentList[name] = newstud;
使用:
不过,这只是一个建议。为了使用函数添加条目,您应该让参数
studentList
通过引用传递
而不是
studentList[name] = newstud;
使用:
不过,这只是一个建议。- 问题1
- 问题2
studentList.insert(std::make_pair(name,newstud))成功插入代码>它不会反映main()
中原始映射studentList
中的更改,除非在createStudent的函数定义和声明中使用引用类型
所以使用
void createStudent(map<string, Student>& );
void createStudent(map&);
- 问题1
如果新元素不存在,将使用默认构造函数将其插入容器中,在您的情况下,默认构造函数不存在,即使您提供了一个,也可能没有意义
因此,请在这种情况下使用
- 问题2
即使您使用studentList.insert(std::make_pair(name,newstud))成功插入代码>它不会反映main()
中原始映射studentList
中的更改,除非在createStudent的函数定义和声明中使用引用类型
所以使用
void createStudent(map<string, Student>& );
void createStudent(map&);
- 问题1
如果新元素不存在,将使用默认构造函数将其插入容器中,在您的情况下,默认构造函数不存在,即使您提供了一个,也可能没有意义
因此,请在这种情况下使用
- 问题2
即使您使用studentList.insert(std::make_pair(name,newstud))成功插入代码>它不会反映main()
中原始映射studentList
中的更改,除非在createStudent的函数定义和声明中使用引用类型
所以使用
void createStudent(map<string, Student>& );
void createStudent(map&);
- 问题1
如果新元素不存在,将使用默认构造函数将其插入容器中,在您的情况下,默认构造函数不存在,即使您提供了一个,也可能没有意义
因此,请在这种情况下使用
- 问题2
即使您使用studentList.insert(std::make_pair(name,newstud))成功插入代码>它不会反映main()
中原始映射studentList
中的更改,除非在createStudent的函数定义和声明中使用引用类型
所以使用
void createStudent(map<string, Student>& );
void createStudent(map&);
阅读一篇文章。另外请注意,对学生列表的更改不会产生任何影响。您只是按值传递。如果您试图传递一个int
参数,并期望函数返回时int
会发生神奇的变化,那么您的错误也不会有什么不同。相反,通过引用传递。另外请注意,对学生列表的更改不会产生任何影响。您只是按值传递。如果您试图传递一个int
参数,并期望函数返回时int
会发生神奇的变化,那么您的错误也不会有什么不同。相反,通过引用传递。另外请注意,对学生列表的更改不会产生任何影响。您只是按值传递。如果您试图传递一个int
参数,并期望函数返回时int
会发生神奇的变化,那么您的错误也不会有什么不同。相反,通过引用传递。另外请注意,对学生列表的更改不会产生任何影响。您只是按值传递。如果您试图传递一个int
参数,并期望函数返回时int
会发生神奇的变化,那么您的错误也不会有什么不同。这不仅仅是一个建议,也不仅仅是一个建议