CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Posts
    41

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

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  3. #3
    Join Date
    Dec 2005
    Posts
    95

    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

  4. #4
    Join Date
    Jan 2008
    Posts
    41

    Re: Errors i can't understand

    Quote 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...

  5. #5
    Join Date
    Jan 2008
    Posts
    41

    Re: Errors i can't understand

    Quote 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?

  6. #6
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Errors i can't understand

    Quote 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

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Errors i can't understand

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured