|
-
July 28th, 2008, 06:16 AM
#1
Errors i can't understand
I use forward declaration to include class interface but get errors. Class Contract uses Class Date...
My Date.cpp
Code:
#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
Code:
// 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....
Code:
: 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...
-
July 28th, 2008, 06:25 AM
#2
Re: Errors i can't understand
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
Code:
Date issued, returned;
so forward declaration will not work
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
July 28th, 2008, 06:50 AM
#3
Re: Errors i can't understand
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
-
July 28th, 2008, 07:33 AM
#4
Re: Errors i can't understand
 Originally Posted by souldog
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
Code:
Date issued, returned;
so forward declaration will not work
Oh! didn't know that, thanx...
-
July 28th, 2008, 07:34 AM
#5
Re: Errors i can't understand
 Originally Posted by thegrinch
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?
-
July 28th, 2008, 08:39 AM
#6
Re: Errors i can't understand
 Originally Posted by c#_novice
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
Old Unix programmers never die, they just mv to /dev/null
-
July 28th, 2008, 09:30 AM
#7
Re: Errors i can't understand
 Originally Posted by c#_novice
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|