这是我的错误
/tmp/cce7wiXI.o:在函数main':
ChronoTest.cpp:(.text+0x652): undefined reference to
Chrono :: operator>>(std :: istream&,Chrono :: Date&)'
collect2:错误:ld返回1退出状态
这是我的头文件
#include <iostream>
using namespace std;
namespace Chrono{
enum class Month{
jan=1,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec
};
class Date{
public:
class Invalid{};
Date(int y, Month m, int d);
Date();
int day() const{return d;}
Month month()const{return m;}
int year()const{return y;}
void add_day(int n);
void add_month(int n);
void add_year(int n);
private:
int y;
Month m;
int d;
};
bool is_date(int y, Month m, int d);
bool leapyear(int y);
bool operator==(const Date& a, const Date& b);
bool operator!=(const Date& a, const Date& b);
ostream& operator<<(ostream& os, const Date& d);
istream& operator>>(istream& is, Date& dd);
Date& operator++(Date& d);
Date operator++(Date& dl, int);
}
这是我的主要文件
#include "Chrono.h"
//------------------------------------------------------------------------------
namespace Chrono{
Date::Date(int yy, Month mm, int dd)
:y{yy},m{mm},d{dd}
{
if(!is_date(yy,mm,dd))throw Invalid();
}
const Date& default_date()
{
static Date dd{2001,Month::jan,1};
return dd;
}
Date::Date()
:y{default_date().year()},
m{default_date().month()},
d{default_date().day()}{}
void Date::add_day(int n){
for(int i = 0; i<n; ++i){
++d;
if(!is_date(y, m, d)){
d = 1;
if(m == Month::dec){
m = Month::jan;
++y;
}else
m = Month(static_cast<int>(m)+1);
}
}
}
void Date::add_month(int n){
}
void Date::add_year(int n){
if (m==Month::feb&& d==29&&!leapyear(y+n)){
m = Month::mar;
d=1;
}
y+=n;
}
bool is_date(int y, Month m, int d)
{
if(d<=0)return false;
if(m<Month::jan || Month::dec<m)return false;
int days_in_month = 31;
switch(m){
case Month::feb:
days_in_month = (leapyear(y))?29:28;
break;
case Month::apr:case Month::jun:case Month::sep:case Month::nov:
days_in_month = 30;
break;
}
if(days_in_month<d)return false;
return true;
}
bool leapyear(int y)
{
return false;
}
bool operator==(const Date& a, const Date& b)
{
return a.year()==b.year()
&&a.month()==b.month()
&&a.day()==b.day();
}
bool operator!=(const Date& a, const Date& b)
{
return !(a==b);
}
ostream& operator<<(ostream& os, const Date& d)
{
return os<<'('<<d.year()
<<','<<static_cast<int>(d.month())
<<','<<d.day()<<')';
}
istream& operator<<(istream& is, Date& dd)
{
int y, m, d;
char ch1, ch2, ch3, ch4;
is>>ch1>>y>>ch2>>m>>ch3>>d>>ch4;
if(!is)return is;
if(ch1!='('||ch2!=','||ch3!=','||ch4!=')'){
is.clear(ios_base::failbit);
return is;
}
dd = Date(y, Month(m), d);
return is;
}
Date operator++(Date& d1, int)
{
Date d2 = d1;
d1.add_day(1);
return d2;
}
enum class Day{
sunday,monday,tuesday,wednesday,thursday,friday,saturday
};
Day day_of_week(const Date& d)
{
}
Date next_Sunday(const Date& d)
{
return d;
}
Date next_weekday(const Date& d)
{
return d;
}
}
int main()
try
{
using namespace Chrono;
cout << "enter some dates: ";
Date d;
cin>> d;
cout<< d << "is a valid date!";
// For some Windows(tm) setups
}
catch (Chrono::Date::Invalid&) {
cerr << "error: Invalid date\n";
// For some Windows(tm) setup
return 1;
}
catch (runtime_error& e) { // this code is to produce error messages
cout << e.what() << '\n';
// For some Windows(tm) setups
}
我试图输入日期来验证它是否是真实的一天。
答案 0 :(得分:1)
标题中的函数签名是:
istream& operator>>(istream& is, Date& dd);
这在您的实施文件中不正确:
istream& operator<<(istream& is, Date& dd)
// ^^ Should be >>