2
編譯器給我以下錯誤消息:「未知類型名稱'ostream'和'未知類型名稱'istream' 還'使用未聲明的標識符'ios_base'。是否應該添加任何?。在頭文件頭的類型或將出現同樣的錯誤太多我不知道什麼是錯的,因爲我從字面上書中複製的代碼(本來) 這裏是我的代碼:ostream&istream&未正確定義
#include "chrono.h"
//The definitions go into Chrono.cpp:
// Chrono.cpp
namespace Chrono {
// member function definitions:
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)
{
//..
}
void Date::add_month(int n){
//..
}
void Date::add_year(int n){
if (m==feb && d==29 && !leapyear(y+n)) { // beware of leap years!
// makes sense for both positive and negative n (n==0 should be impossible here)
m = mar; // use March 1 instead of February 29
d = 1;
}
y+=n;
}
// helper functions
bool is_date(int y, Month m, int d)
{
// assume that y is valid
if (d<=0) return false; // d must be positive
if (m<Month::jan || Month::dec<m) return false;
int days_in_month = 31; // most months have 31 days
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)
{
// see exercise 10
}
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()
<< ',' << 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!=')') { // oops: format error
is.clear(ios_base::failbit); // set the fail bit
return is;
}
dd = Date(y,Month(m),d); // update dd
return is;
}
enum class Day {
sunday,monday,tuesday,wednesday,thursday,friday,saturday
};
Day day_of_week(const Date& d)
{
//..
}
Date next_Sunday(const Date& d)
{
//..
}
Date next_weekday(const Date& d)
{
//..
}
}
這是頭文件
//chrono.h
namespace Chrono {
enum Month {
jan=1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};
class Date {
public:
class Invalid { }; // to throw as exception
Date(int y, Month m, int d); // check for valid date and initialize
Date(); // default constructor
// the default copy operations are fine
// non-modifying operations:
int day() const { return d; }
Month month() const { return m; }
int year() const { return y; }
// modifying operations:
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); // true for valid date
bool leapyear(int y); // true if y is a leap year
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 day_of_week(const Date& d); // day of week of d
Date next_Sunday(const Date d); // next Sunday after d
Date next_weekday(const date& d); // next weekday after d
}
'#include' –
Barmar