Strange compile errors

This is a discussion on Strange compile errors within the C++ Programming forums, part of the General Programming Boards category; I get some errors i can't interpret.. Code: // Class CONTRACT: Handles the rental contract details #include <string> #include <iosfwd> ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Strange compile errors

    I get some errors i can't interpret..

    Code:
    // Class CONTRACT: Handles the rental contract details
    
    #include <string>
    #include <iosfwd>
    
    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); // First 3 errors points to this declaration 
    
    	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);
    Code:
    [errors]
    : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    : error C2751: 'std' : the name of a function parameter cannot be qualified
    : error C2146: syntax error : missing ',' before identifier 'string'
    : 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
    etc..
    Here's my Date Class

    Code:
    // Class DATE : Handles the car return and issue dates
    #include <time.h>
    
    #ifndef DATE_H
    #define DATE_H
    
    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;
    	}
    
    	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;
    };
    
    // Get the local date and time of system
    std::tm getCurrentTime()
    {
    	std::time_t rawtime;
    	std::tm* timeinfo;
    
    	std::time( &rawtime );
    	timeinfo = std::localtime ( &rawtime );
    
    	return *timeinfo;
    }
    
    #endif
    Last edited by csonx_p; 07-28-2008 at 05:51 AM.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    Code:
    const::std string&
    There's your typo for the first three errors.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by rags_to_riches View Post
    Code:
    const::std string&
    There's your typo for the first three errors.
    Thanks, any ideas why Contract can't see Date?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,157
    A forward declaration of Date is not enough since Contract stores a Date object.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by laserlight View Post
    A forward declaration of Date is not enough since Contract stores a Date object.
    If that's the case why is the following code compiling?
    Code:
    // Class menu
    #ifndef MENU_H
    #define MENU_H
    
    #include <string>
    #include <vector>
    
    class Fleet;
    
    class Menu 
    {
    private:
    	typedef void (Menu::*Func)();
    
    	struct Item  {
    		std::string	descr;
    		char        choice;
    		Func		func;
    	};
    
    public:
    	Menu(Fleet&);
    	~Menu();
    
    	void Create();
    	void addItem(const std::string& s, char c, Func f);
    	void Menu::printItem(const Item& item) const; 
    	std::string Menu::whiteSpace(const Item& item, int slenght, char space) const;
    
    	void choose();
    
    	// Menu operation functions
    	void Exit();
    	void Pause();
    	void Clrscr();
    	void StartScreen();
    	void AddCarOption();
    	void EditCarOption();
    	void ListCarsOption();
    	void ReadFileOption();
    	void SearchCarOption();
    	void UpdateCarOption();
    	void DeleteCarOption();
    	void TitleDescription();
    	bool done() { return finished; }
    
    private:
    	Fleet& fleet;  // <-- HERE ... This compiles
    	typedef std::vector<Item> ItemVector;
    	ItemVector items;
    	typedef ItemVector::iterator itemIterator;
    	bool finished;
    };
    #endif

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Quote Originally Posted by csonx_p
    If that's the case why is the following code compiling?
    What source file includes that header and what do the includes portion of that source file look like? If any source file that includes that header also includes the Fleet header before this one, then it's likely that things will compile. The header file by itself isn't really compiled, it is cut and pasted into the relevant source file(s) which is then compiled so asking why a header file (by itself) is (un)successfully compiled or not doesn't mean much in that context.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by csonx_p View Post
    If that's the case why is the following code compiling?

    Fleet& fleet; // <-- HERE ... This compiles
    Right - it's a reference you're storing, not a Fleet object. A forward declaration is enough in this case.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by hk_mp5kpdw View Post
    What source file includes that header and what do the includes portion of that source file look like? If any source file that includes that header also includes the Fleet header before this one, then it's likely that things will compile. The header file by itself isn't really compiled, it is cut and pasted into the relevant source file(s) which is then compiled so asking why a header file (by itself) is (un)successfully compiled or not doesn't mean much in that context.
    The answer to the question i had is that forward declaration are for pointers or references to objects... Date issued VS Fleet& fleet... Hence the latter compiles compared to the other

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    The thing is that when you are using non-references and non-pointers, then the compiler must see the definition of the class, so it can determine such things as if there's an appropriate constructor, and such.
    If you use references or pointers, however, the compiler can compile the code without knowing the contents of them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Most of all it needs to know the size of the instance, whereas I suppose the size of pointers/references is always the same/known to compiler.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,915
    True, a pointer and reference are always fixed size, whereas a class is not.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile errors in stl header files
    By Swordsalot in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2008, 03:41 AM
  2. beginner c++ programmer compile errors
    By dodo10 in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2008, 05:37 PM
  3. Visual Studio 2005 - Debug code with compile errors?
    By Swerve in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-17-2008, 09:12 AM
  4. Compile Errors for CreateThread..
    By gopi_tony in forum Windows Programming
    Replies: 20
    Last Post: 10-22-2006, 10:58 AM
  5. Dev-c++ 4.0 compile errors
    By DigitalNOISE in forum C++ Programming
    Replies: 9
    Last Post: 07-12-2002, 05:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21