关于容器类map的问题(84个warning,汗死~~~~)
由以下代码引出的问题。用学生姓名作关键字,与Student对象建立映射。重载了两个<<运算符用于输出学生姓名和年龄。VC上可以通过编译,但竟然有84个warning!#i...
由以下代码引出的问题。
用学生姓名作关键字,与Student对象建立映射。重载了两个<<运算符用于输出学生姓名和年龄。VC上可以通过编译,但竟然有84个warning!
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Student
{
public:
Student():itsName("John"),itsAge(18) {}
Student(const string, int );
const string & GetName() const {return itsName;}
int GetAge() const {return itsAge;}
void SetName(const string name) {itsName=name;}
void SetAge(int age) {itsAge=age;}
//friend ostream & operator<<(ostream &,const Student &);
private:
string itsName;
int itsAge;
};
Student::Student(const string name,int age):
itsName(name),itsAge(age)
{}
ostream & operator<<(ostream &theStream,const Student &rhs)
{
theStream<<rhs.GetName()<<endl;
theStream<<rhs.GetAge()<<endl;
return theStream;
}
ostream & operator<<(ostream &theStream,const map<string,Student> &rhs)
{
for(map<string,Student>::const_iterator ci=rhs.begin();
ci !=rhs.end();
ci++)
theStream<<ci->second<<endl;
return theStream;
}
typedef map<string,Student> SchoolClass;
int main()
{
Student Mike("Mike",15);
Student Jane("Jane",13);
Student Cathy("Cathy",18);
Student Bob("Bob",12);
Student Peter("Peter",10);
map<string,Student> ourClass; //这样定义映射有错?
cout<<ourClass.size()<<endl;
ourClass["Mike"]=Mike;
ourClass["Jane"]=Jane;
ourClass["Cathy"]=Cathy;
ourClass["Bob"]=Bob;
cout<<ourClass.size()<<endl;
cout<<ourClass<<endl;
return 0;
} 展开
用学生姓名作关键字,与Student对象建立映射。重载了两个<<运算符用于输出学生姓名和年龄。VC上可以通过编译,但竟然有84个warning!
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Student
{
public:
Student():itsName("John"),itsAge(18) {}
Student(const string, int );
const string & GetName() const {return itsName;}
int GetAge() const {return itsAge;}
void SetName(const string name) {itsName=name;}
void SetAge(int age) {itsAge=age;}
//friend ostream & operator<<(ostream &,const Student &);
private:
string itsName;
int itsAge;
};
Student::Student(const string name,int age):
itsName(name),itsAge(age)
{}
ostream & operator<<(ostream &theStream,const Student &rhs)
{
theStream<<rhs.GetName()<<endl;
theStream<<rhs.GetAge()<<endl;
return theStream;
}
ostream & operator<<(ostream &theStream,const map<string,Student> &rhs)
{
for(map<string,Student>::const_iterator ci=rhs.begin();
ci !=rhs.end();
ci++)
theStream<<ci->second<<endl;
return theStream;
}
typedef map<string,Student> SchoolClass;
int main()
{
Student Mike("Mike",15);
Student Jane("Jane",13);
Student Cathy("Cathy",18);
Student Bob("Bob",12);
Student Peter("Peter",10);
map<string,Student> ourClass; //这样定义映射有错?
cout<<ourClass.size()<<endl;
ourClass["Mike"]=Mike;
ourClass["Jane"]=Jane;
ourClass["Cathy"]=Cathy;
ourClass["Bob"]=Bob;
cout<<ourClass.size()<<endl;
cout<<ourClass<<endl;
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询