c++ - ostream& 和 istream& 未正确定义

标签 c++

编译器给我以下错误信息:“Unkown type name 'ostream' and 'Unknown type name '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)
    {
        //..
    }

}

这是头文件

//计时.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 <iosfwd>

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

在 cpp 中 - 你将使用

#include <iostream> 

按照 Barmar 的建议。并为此命名空间的成员指定命名空间 std::。

关于c++ - ostream& 和 istream& 未正确定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577620/

相关文章:

c++ - DbgHelp.dll 是 Windows 内置的吗?我可以相信它在那里吗?

c++ - 如何在 OpenCL 中使用结构

c# - P/从 c# 调用非托管 C++ 代码 - 获取 "tried to access protected memory error"

c++ - 两种不同的编译器配置之间可能会丢失精度

c++ - 使用 find 搜索字符串中是否存在空格

c++ - 字符串不命名类型

c++ - 标识符表

c++ - 只为一种widget设置样式

c++ - cudaMemcpy 函数调用中的“cudaErrorUnknown”

c++ - 编译错误 : Directory has same name as c++ standard header <list>