www.delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/05/11/15:17:59

X-Spam-Check-By: sourceware.org
Date: Thu, 11 May 2006 19:17:38 +0000 (UTC)
From: "Hammond, Robin-David%KB3IEN" <muaddib AT databit7 DOT com>
To: Klaas Thoelen <klaas DOT thoelen AT telenet DOT be>
cc: cygwin AT cygwin DOT com
Subject: Re: how come #include "*.cpp" works?
In-Reply-To: <44638BED.5020009@telenet.be>
Message-ID: <Pine.NEB.4.64.0605111915120.17618@andromeda.ziaspace.com>
References: <44638BED DOT 5020009 AT telenet DOT be>
MIME-Version: 1.0
X-Greylist: Sender is SPF-compliant, not delayed by milter-greylist-2.1.2 (andromeda.ziaspace.com [127.0.0.1]); Thu, 11 May 2006 12:17:38 -0700 (PDT)
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

It looks like you have included one file two times. Thus upon the second 
include your are redefining that which has definition. Anytime you do 
that, the compiler must stop you, even if the redefinition matches the 
existing one.



On Thu, 11 May 2006, Klaas Thoelen wrote:

> Date: Thu, 11 May 2006 21:09:33 +0200
> From: Klaas Thoelen <klaas DOT thoelen AT telenet DOT be>
> To: cygwin AT cygwin DOT com
> Subject: how come #include "*.cpp" works?
> 
> 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;
> }
>


Some people are born mediocre, some people achieve mediocrity, and some
people have mediocrity thrust upon them.
                 -- Joseph Heller, "Catch-22"



Robin-David Hammond	KB3IEN
 	www.aresnyc.org.

--
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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019