然后:cin>>year;//1998
最后我想抽取世纪数以及年份数,该怎样做?
例如:在上面分别提取19世纪和98年。
还请教一下,下面错在哪?
#include <iostream>
#include <string>
using namespace std;
class Month
{
public:
Month(){};
Month(string month,int day,int year);
string getMonth()const;
int getDay()const;
int getYear()const;
private:
string TheMonth;
int TheDay,
TheYear;
};
Month::Month(string month,int day,int year)
{
string TheMonth=month;
int TheDay=day,
TheYear=year;
}
inline string Month::getMonth()const
{
if (TheMonth=="March")
return 1;
else if(TheMonth=="April")
return 2;
else if(TheMonth=="May")
return 3;
else if(TheMonth=="June")
return 4;
else if(TheMonth=="July")
return 5;
else if(TheMonth=="August")
return 6;
else if(TheMonth=="September")
return 7;
else if(TheMonth=="October")
return 8;
else if(TheMonth=="November")
return 9;
else if(TheMonth=="DeceMber")
return 10;
else if(TheMonth=="January")
return 11;
else if(TheMonth=="February")
return 12;
}
错误信息:error C2664: '__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(co
nst class std::allocator<char> &)' : cannot convert parameter 1 from 'const int' to 'const class std::allocator<char> &'
Reason: cannot convert from 'const int' to 'const class std::allocator<char>'
No constructor could take the source type, or constructor overload resolution was ambiguous
很郁闷,又有点小问题了,帮忙找下 呵呵。
类文件:
#include <iostream>
#include <string>
using namespace std;
class Month
{
public:
Month(){};
Month(string month,int day,int year);
int getMonth()const;
int getDay()const;
int getYear()const;
int YearMonthDay()const;
void print(ostream& out)const;
private:
string TheMonth;
int TheDay,
TheYear;
};
Month::Month(string month,int day,int year)
{
string TheMonth=month;
int TheDay=day,
TheYear=year;
}
inline int Month::getMonth()const
{
if (TheMonth=="March")
return 1;
else if(TheMonth=="April")
return 2;
else if(TheMonth=="May")
return 3;
else if(TheMonth=="June")
return 4;
else if(TheMonth=="July")
return 5;
else if(TheMonth=="August")
return 6;
else if(TheMonth=="September")
return 7;
else if(TheMonth=="October")
return 8;
else if(TheMonth=="November")
return 9;
else if(TheMonth=="DeceMber")
return 10;
else if(TheMonth=="January")
return 11;
else if(TheMonth=="February")
return 12;
}
inline int Month::getDay()const
{
return TheDay;
}
inline int Month::getYear()const
{
return TheYear;
}
inline int Month::YearMonthDay()const
{
return getYear()+' '
+getMonth()+' '
+getDay();
}
inline void Month::print(ostream& out)const
{
cout<< getYear()+' '
+getMonth()+' '
+getDay();
}
/*****************************/
驱动程序:
#include<iostream>
#include<string>
#include"Month.h"
using namespace std;
void main()
{
Month year("May",23,1998);
cout <<year.YearMonthDay <<endl;
}
/**************************/
警告信息:
D:\学习软件\c++1\MSDev98\MyProjects\类深入\类.cpp(8) : warning C4761: integral size mismatch in argument; conversion supplied
d:\学习软件\c++1\msdev98\myprojects\类深入\month.h(54) : warning C4715: 'Month::getMonth' : not all control paths return a value
Linking...
/********************************/
为什么我的驱动程序怎样改,最后结果都是:1