map中结构体的赋值
/************************************************************************//* pair对象的创建和使用 *//************************************************************************/#include <iostream>#include <utility>#include <map>#include <string.h>using namespace std;class Student {public: Student() { strcpy(m_sName, "NULL"); m_nAge = 0; } Student(char sName[20], int nAge) { strcpy(m_sName ,sName); m_nAge = nAge; } Student(Student& s1) { strcpy(m_sName, s1.GetName()); m_nAge = s1.GetAge(); } int GetAge() { return m_nAge; } char * GetName() { return m_sName; }private: char m_sName[20]; unsigned int m_nAge; };int main(){ Student s1("sdd",3); map<int, Student> m1; m1.insert(pair<int, Student>(12, s1) ); return 0;}class Student {public: Student() { strcpy(m_sName, "NULL"); m_nAge = 0; } Student(int a) { m_nAge = a; } Student(const char*sName, int nAge) { strcpy(m_sName ,sName); m_nAge = nAge; } Student(const Student& s1) { strcpy(m_sName, s1.m_sName); m_nAge = s1.m_nAge; } int GetAge() { return m_nAge; } char * GetName() { return m_sName; }private: char m_sName[20]; unsigned int m_nAge; };int main(){ // Student s1; map<int, Student> m1; m1.insert(pair<int,Student>(12, Student("lilin", 3)) ); system("pause"); return 0;}
[解决办法]
返回值类型改成 const char*就是了