我正在尝试为类编写头文件,但每当我使用命令g++ test.cpp Date.c
链接文件时,我得到的输出为1
以下是日期类
的头文件和.cpp文件的代码Date.h
#include<string.h>
using namespace std;
#ifndef DATE_H
#define DATE_H
/* string temp[]={ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
// vector<string> months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
vector<string> months(temp,temp+sizeof(temp)/sizeof(temp[0]));
*/
string intToString(const int& n);
class Date{
//Private members
string month;
int day;
int year;
bool isValidMonth() const;
int daysInMonth(const string& m) const;
bool isLeapYear() const;
public:
friend istream& operator >>(istream& is, Date& d);
friend ostream& operator <<(ostream&os ,const Date& d);
//public constructor
Date(const string& m="January",const int& d=1,const int& y=2000);
void setMonth(const string& m);
void setDay(const int& d);
void setYear(const int& y);
string getMonth()const;
int getDay() const;
int getYear() const;
void Month();
bool isValidDate() const;
string toString();
};
#endif
Date.cpp
#include<iostream>
#include<vector>
#include<string.h>
#include<ctype.h>
#include<algorithm>
#include "Date.h"
using namespace std;
string temp[]={ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
// vector<string> months{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
vector<string> months(temp,temp+sizeof(temp)/sizeof(temp[0]));
string intToString(const int& n){
return to_string(n); //inbuilt function
//return string(itoa(n));
}
bool Date::isValidMonth() const{
if(day>=1 && day<=daysInMonth(month)){
return true;
}
else{
return false;
}
}
int Date::daysInMonth(const string& m) const{
vector<string>::iterator m_index;
// while(find(months.begin(),months.end(),m)!=months.end()){
m_index=find(months.begin(),months.end(),m);
// }
int month_index=distance(months.begin(),m_index)+1;
switch(month_index)
{
case 2: return isLeapYear()?29:28;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
bool Date::isLeapYear() const{
if(year%4==0){
if(year%100==0 && year%400==0){ //2000 is leap year but 1900 is not
return true;
}
else{
return false;
}
}
else{
return false;
}
}
Date::Date(const string& m,const int& d,const int&y){
month=m;
day=d;
year=y;
}
void Date::setMonth(const string& m){
month=m;
}
void Date::setDay(const int& d){
day=d;
}
void Date::setYear(const int& y){
year=y;
}
string Date::getMonth()const{
return month;
}
int Date::getDay() const{
return day;
}
int Date::getYear() const{
return year;
}
void Date::Month(){
if(!month.empty())
{
month[0]=toupper( month[0] );
for(int i=1;i<month.length();++i)
month[i]=tolower(month[i]);
}
}
bool Date::isValidDate() const{
//calls isValidMonth() to check if days match
if(isValidMonth() ){
return true;
}
return false;
}
string Date::toString(){
string result_date;
result_date=intToString(day)+"-"+month+"-"+intToString(year);
return result_date;
}
istream & operator >>(istream & is, Date& d){
//overloads the >>operator, which reads
cout<<"\n Enter the month of the date \n";
cin>>d.month;
cout<<"\n Enter the day of the date \n";
cin>>d.day;
cout<<"\n Enter the year of the date \n";
cin>>d.year;
return is;
}
ostream & operator <<(ostream & os ,const Date& d){
os<<d.month<<" "<<d.day<<","<<d.year;
return os;
}
当我从.h文件中删除string.h时,我得到string does not name a type error
以及如何申报朋友职能
istream & operator >>(istream & is, Date& d){
//overloads the >>operator, which reads
cout<<"\n Enter the month of the date \n";
cin>>d.month;
cout<<"\n Enter the day of the date \n";
cin>>d.day;
cout<<"\n Enter the year of the date \n";
cin>>d.year;
return is;
}
和const函数bool isValidMonth() const;