Thread: Help calling a function

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    Help calling a function

    Alright this mite b kind of long so thx in advance if u can help me i really really appreciate this. my program was already done. its an association and containment program between pets and their owners and ive done all the stuff already except the owners birthdate.. I now have to use the schools Date class to output the owners birthdate and im havin trouble doing that. i had it working b4 using their class now icant get it. Its alot of diff files ill post what i think shows the prob and if u need more ill post it.

    here is the date.cpp file i wont post the .h b/c its pretty much the same ig uess but w/ ou the definitions

    Date.cpp - THIS CANT B CHANGED i didnt write this lol
    Code:
    
    #include "Date.h"
    #include <iostream>
    #include <cstdlib>	
    
    // When we're not in a header file, it is ok to
    // have "using namespace std"
    using namespace std;
    namespace CS1124{
    Date::Date(string date) {
    	// atoi comes
    	month = atoi(date.substr(0,2).c_str());
    	day = atoi(date.substr(3,2).c_str());
    	year = atoi(date.substr(6,4).c_str());
    }
    void Date::display(std::ostream& os) const {
    	os << month << '/' << day << '/' << year;
    }
    bool Date::earlierThan(Date d) const {
    	if (year > d.year)
    		return false;
    	else if (year < d.year)
    		return true;
    	else if (month > d.month)
    		return false;
    	else if (month < d.month)
    		return true;
    	else if (day >= d.day)
    		return false;
    	else 
    		return true;
    }
    int Date::getYear() const {
    	return year;
    }
    void Date::setYear(int x) {
    	year = x;
    }
    }
    here is my owners.h - ive cut it down
    Code:
     
    #ifndef OWNER_H
    #define OWNER_H
    #include "Pet.h"
    #include "Date.h"
    #include <string>
    
    namespace CS1124
    {
    	class Owner
    	{
    		public:
    			//constructor initializes the name of the owner and the birthdate
    			Owner(std::string ownerName, std::string date);
    
    			
    		void displayDate ();
    			
    		private:
    			//int month, day, year;
    			std::string ownerName; //owners name
    			Date date; //from date class. will get the birthdate
    			Pet* pet; // pointer to the pet class for a pet
    	};
    	//Overload << to output the information
    	std::ostream& operator << (std::ostream & os,const Owner& owner);
    	
    }
    #endif
    and the cpp cut down as well
    Code:
    #include "Owner.h"
    #include "Date.h"
    using namespace std;
    
    namespace CS1124
    {
    	//constructor intializes the owners name, the birthdate from the date class
    	//as well as setting the owner to have no initial pet
    	Owner::Owner(std::string ownerName, std::string date):
    	ownerName(ownerName), date(date),	pet(NULL)
    	{}
    
    	//output the owners and pets names
    
    	void Owner::displayDate()
    	{
    		date.display();
    	}
    
    	ostream& operator << (ostream & os,const Owner& owner)
    	{
    //--------------------------------------------
    //THIS IS WHERE MY PROBLEM ISS!!!!!!!
    //owner.displaydate in specific
    //-----------------------------------------------
    		//display the owner
    		os<< "Owner: " << owner.getName() 
    			<< " DOB: " <<" " << owner.displayDate();
    		os << " My Pet: ";
    		//Check if he has a pet
    		if(owner.getPet())//display that pet
    			os<<owner.getPet()->getName();
    		else //if not say he doesnt
    			os<<"I don't have a pet.";
    
    		return os;
    	}
    
    
    }
    im sorry its so long - basically i just want to display the date and i have to use the date class to get it and display... ive tried doing
    date.display() in the overload << function that didnt work.
    i tried now as u can c making a display func inside of owner that dislays the date but thast not working any help is appreciated.. im aslo going to attach the files incase any1 gets really bored.

    BTW i coulnt upload Pet.h and pet.cpp b/c im liimted to 5 but i can in another post if any1 wants it lol i doubt it
    Last edited by gamer4life687; 10-13-2005 at 08:58 PM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Date.Display() takes an ostream reference. Your displayDate method should also take an ostream reference the same way and pass it along to date.Display(). Then you can pass os to displayDate.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    i think i understand wat ure sayin ..but could u show me a lil im havin a hard time with this one
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The attached *.txt file is really *.zip -- rename it to *.zip then uncompress with winzip or pkzip.

    I made up my own pet.h file, so it contains only enough to satisfy the compiler. I made enough changes in the other files to let VC++ 6.0 compile it without error/warnings.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    well i understand what u guys mean now but when i took that function and used it i get this error

    this one points to date.display(os);
    Code:
    c:\Documents and Settings\Mine\Desktop\NEW HW03\HW03 NEW\hw03\Owner.cpp(17): error C2664: 'CS1124::Date::display' : cannot convert parameter 1 from 'std::ofstream' to 'std::ostream &'
    and this one 2 <<owner.displaydate(os); of course
    Code:
    c:\Documents and Settings\Mine\Desktop\NEW HW03\HW03 NEW\hw03\Owner.cpp(25): error C2664: 'CS1124::Owner::displayDate' : cannot convert parameter 1 from 'std::ostream' to 'std::ofstream &'
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    All your functions should take an ostream&, not an ofstream. That is because an ofstream is an ostream (just a more advanced version for files), so you can pass your ofstream variable to a function that expects an ostream&. I'm just guessing that this is your problem.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    ok ive got it working .. well almost

    it displays the date for example like this
    5/1/192600460778
    that 00460778 i have not idea what that it but it displays it afta each date... any ideas...

    ive zipped the program just rename it.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    I was tryin to debug this thing this morn and when it gets to the line

    Code:
    Owner mm("marilyn", "05/01/1926");
    thats when the junk 0490809 blah gets initialized.
    I'm guessing then it could be something with the constructor of Owner when try to initiliaze date. Any clues? im so close!! lol

    heres the code btw

    Code:
    class Owner
    	{
    		public:
    			//constructor initializes the name of the owner and the birthdate
    			Owner(std::string ownerName, std::string date);
    
    			//getters
    			//get the name of the owner
    			std::string getName()const{return ownerName;}
    			//get the pet if he has one
    			Pet* getPet()const{return pet;}
    		
    			//when he buys the pet, the owner needs the name of the pet
    			//change the status of the pet owner
    			void buyPet(Pet& p,const std::string name);
    	
    			//Again change status for the pet running away
    			void runaway();
    			
    			//ostream output for display() from date.h
    			//displays the date
    			
    			std::ostream& displayDate (std:: ostream& os) const;
    	
    		private:
    			//int month, day, year;
    			std::string ownerName; //owners name
    			Date date; //from date class. will get the birthdate
    			Pet* pet; // pointer to the pet class for a pet
    	};
    	//Overload << to output the information
    	std::ostream& operator << (std::ostream & os,const Owner& owner);
    and owner.cpp
    Code:
    //constructor intializes the owners name, the birthdate from the date class
    	//as well as setting the owner to have no initial pet
    	Owner::Owner(std::string ownerName, std::string date):
    	ownerName(ownerName), date(date), pet(NULL)
    	{}
    
    	std::ostream& Owner::displayDate (std::ostream& os) const
    	{
    		date.display(os);
    		return os;
    	}
    
    	//output the owners and pets names
    	ostream& operator << (ostream & os, const Owner& owner)
    	{
    		//display the owner
    		os<< "Owner: " << owner.getName() 
    				<< " DOB: " <<" "; 
    		os << owner.displayDate(os);
    		os << " My Pet: ";
    		//Check if he has a pet
    		if(owner.getPet())//display that pet
    			os<<owner.getPet()->getName();
    		else //if not say he doesnt
    			os<<"I don't have a pet.";
    
    		return os;
    	}
    i cut it down again for posting purposes.. the file stuff is in the zip in my previous post thx
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    os << owner.displayDate(os);
    You don't need the os << in that code. Just call the function, it will do the output for you.

    The extra junk is when the os << tries to output the return value of displayDate, which in this case is an ostream&. I guess that looks like 00460778 when you output it.

  10. #10
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    thx every1 its working good now
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM