ostream的和放;和istream&没有正确定义

时间:2017-10-05 03:58:15

标签: c++

编译器给出了以下错误消息:"未知类型名称' ostream'和'未知的类型名称' istream'  并且还使用未声明的标识符' ios_base'。我应该添加任何类型的标题或?同样的错误也出现在头文件中。我不知道出了什么问题,因为我真的从书中复制了代码。 (本来应该)  这是我的代码:

#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
}                                         

1 个答案:

答案 0 :(得分:0)

在标题中,您必须添加

#include <iosfwd>

转发声明iostreams。并使用std::ostreamstd::istream

在cpp中 - 您将使用

#include <iostream> 

正如巴马尔所建议的那样。并为此命名空间的成员指定namespace std ::。