Click to See Complete Forum and Search --> : Errors i can't understand


c#_novice
July 28th, 2008, 06:16 AM
I use forward declaration to include class interface but get errors. Class Contract uses Class Date...

My Date.cpp

#include <time.h>
#include <iosfwd>

class Date
{
public:

Date() {
SetDate( getCurrentTime() );
}

void SetDate(tm curr_tm)
{
day = curr_tm.tm_mday;
month = curr_tm.tm_mon;
year = curr_tm.tm_year;
hour = curr_tm.tm_hour;
minute = curr_tm.tm_min;
local_dt = curr_tm;
}

static tm getCurrentTime()
{
time_t rawtime;
tm* timeinfo;

time( &rawtime );
timeinfo = localtime ( &rawtime );

return *timeinfo;
}

int getDay(){return day;}
int getMonth(){return month;}
int getYear(){return year;}
int getHour(){return hour;}
int getMinute(){return minute;}
tm getLocalDT(){return local_dt;}

private:

int day;
int month;
int year;
int hour;
int minute;
tm local_dt;
};



Contract.h

// Class CONTRACT: Handles the rental contract details

#include <string>
#include <iosfwd>
//#include <iomanip>
//#include <fstream>
//#include <iostream>
//#include "Date.h"

class Date;

class Contract
{
public:

Contract();

void SetContractDetails(Date i, unsigned carR, const std::string& kms, float ratePD,
unsigned lndrID, const std::string& contID, int days);

void SetContractPeriod(Date i_dt, Date r_dt, int d, bool isLtd);
void SetReturnDate();
void DisplayContract();
void Read(std::istream&, Contract&, bool);
void Write(std::ostream&, bool);
const std::string& getContractID() const;
int getContractPeriod() const;
float getRatePerDay() const;
const std::string& getKmType() const;
Date getIssueDate() const;
Date getReturnDate() const;
unsigned getLenderID() const;
unsigned getCarReg() const;

private:
std::string contractID;
Date issued, returned;
unsigned lenderID;
unsigned carRegistration;
int contractDays;
std::string kmType;
float dailyRate;
};
std::istream& operator >> (std::istream& in, Contract& rental);


Errors....

: error C2079: 'Contract::issued' uses undefined class 'Date'
: error C2079: 'Contract::returned' uses undefined class 'Date'
: error C2228: left of '.getDay' must have class/struct/union
1> type is 'int'
: error C2228: left of '.getMonth' must have class/struct/union
1> type is 'int'
: error C2228: left of '.getYear' must have class/struct/union
1> type is 'int'
: error C2228: left of '.getHour' must have class/struct/union
1> type is 'int'

etc...

souldog
July 28th, 2008, 06:25 AM
you can only use forward declaration if the header file only includes pointers or references to the class that is forward declared.

Your header includes objects


Date issued, returned;


so forward declaration will not work

thegrinch
July 28th, 2008, 06:50 AM
Hi!

1. Do you also have a file Date.h?
2. uncomment the line #include "date.h" in your Contract.h
3. Make sure to have a #pragma once on top of all your header files.

Regards
Oliver

c#_novice
July 28th, 2008, 07:33 AM
you can only use forward declaration if the header file only includes pointers or references to the class that is forward declared.

Your header includes objects


Date issued, returned;


so forward declaration will not work

Oh! didn't know that, thanx...

c#_novice
July 28th, 2008, 07:34 AM
Hi!

1. Do you also have a file Date.h?

Regards
Oliver
No, i made my date.h a date.cpp since there isn't much done there? Should i change it back since i need to include it?

HighCommander4
July 28th, 2008, 08:39 AM
No, i made my date.h a date.cpp since there isn't much done there? Should i change it back since i need to include it?

It's the other way around: if your class methods are all very short, you can inline their implementations and put them in the header (.h) file. Then you include that header file weherever it's necessary, e.g. in Contract.h

So basically, just rename date.cpp back to date.h, and include date.h in Contract.h

Lindley
July 28th, 2008, 09:30 AM
Oh! didn't know that, thanx...

The reason being that the compiler needs to know the size of the member object. The size of any pointer or reference is always equal to sizeof(void*), but an actual object's size is definition-dependent.