how come #include "*.cpp" works?

Klaas Thoelen klaas.thoelen@telenet.be
Thu May 11 19:12:00 GMT 2006


Hello *,

I recently installed cygwin to freshen up my C/C++, what I didn't want 
to do using huge programs like visual studio. It seemed to work fine 
until I came across the following problem.

I have 3 files:     datum.h
                         datum.cpp
                         datumprint.cpp (which has my main in it)

In 'datum.cpp' I include 'datum.h' as I should, and in 'datumprint.cpp' 
also. But this gives me compile-errors about members of my class Date 
being undefined. However, if I include 'datum.cpp' in 'datumprint.cpp' 
it works just fine!

This seems a little strange to me. Does anybody know what's wrong here?

Thanks and regards,
Klaas Thoelen



datum.h
******
class Date {
public:
    Date();
    Date(int dd, int mm, int yy);
    int day();
    int month();
    int year();
    static void set_default(int, int, int);
    Date& add_year(int);
    Date& add_month(int);
    Date& add_day(int);
    Date& print();
private:
    int d, m, y;
    static Date default_date;
    static bool is_leapyear(int);
    static int daysinmonth(int, int);
};


datum.cpp
********
#include "datum.h"
#include <iostream>
#include <assert.h>
using namespace std;

Date Date::default_date(27, 4, 2006);

Date::Date() {
    d = default_date.d;
    m = default_date.m;
    y = default_date.y;
}

Date::Date(int dd, int mm, int yy) {
    assert ( !(dd < 1) && !(dd > daysinmonth(mm, yy)) && !(mm < 1) && 
!(mm > 12) );

    d = dd;
    m = mm;
    y = yy;
}

void Date::set_default(int dd, int mm, int yy) {
    assert ( !(dd < 1) && !(dd > daysinmonth(mm, yy)) && !(mm < 1) && 
!(mm > 12) );
   
    default_date.d = dd;
    default_date.m = mm;
    default_date.y = yy;
}

int Date::day() {
    return d;
}

int Date::month() {
    return m;
}

int Date::year() {
    return y;
}

Date& Date::add_year(int yy) {
    y = y + yy;
   
    if ( d == 29 && m == 2 && !is_leapyear(y) ) {
        d = 1;
        m = 3;
    }
    return *this;
}

Date& Date::add_month(int mm) {
    m = m + mm;

    while (m > 12) {
        m = m - 12;
        y++;
    }

    if ( d == 29 && m == 2 && !is_leapyear(y) ) {
        d = 1;
        m = 3;
    }

    if ( d > daysinmonth(m, y)) {
        d = d - daysinmonth(m, y);
            m++;
    }

    return *this;
}

Date& Date::add_day(int dd) {
    d = d + dd;
   
    while (d > daysinmonth(m, y)) {
        if (m == 12) {
            y++;
            d = d - daysinmonth(m, y);
            m = 1;
        } else {                       
            d = d - daysinmonth(m, y);
            m++;
        }
    }
    return *this;
}

Date& Date::print() {
    cout << d << "/" << m << "/" << y << "\n";
    return *this;
}   

bool Date::is_leapyear(int yy) {
    if (( !(yy % 4) && (yy % 100)) || !(yy % 400)) {
        return true;
    }
    return false;
}

int Date::daysinmonth(int mm, int yy) {
    if ( (mm == 2) && is_leapyear(yy) ) {
        return 29;
    } else{   
        int nr_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        return nr_days[mm-1];
    }
}

datumprint.cpp
***********

#include "datum.h"         /***********the problem*************/
#include <iostream>

using namespace std;

void print(Date x) {
    cout << x.day() << "/" << x.month() << "/" << x.year() << "\n";
}

int main() {
    Date::set_default(2, 12, 2007);
    Date x;
    print(x);
    x.add_day(365);
    print(x);

    Date y(22, 12, 1999);
    y.add_day(70);
    print(y);

    Date z(29, 2, 1896);
    z.add_year(4);
    print(z);

    Date r(29, 2, 1896);
    r.add_month(24);
    print(r);

    Date s(31, 05, 1896);
    s.add_month(25);
    print(s);

    return 0;
}
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: cygcheck.out
URL: <http://cygwin.com/pipermail/cygwin/attachments/20060511/d76cb344/attachment.ksh>
-------------- next part --------------
--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


More information about the Cygwin mailing list