Overloading operators

This is a discussion on Overloading operators within the C++ Programming forums, part of the General Programming Boards category; i've been looking over my work more and i think i got the hang of all that other stuff i've ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    45

    Overloading operators

    i've been looking over my work more and i think i got the hang of all that other stuff i've been doing...but this overloading stuff is a little confusing. would this program be right if i was trying to just find the 3 things out about jobA and display them? i want to make sure i get this part right before i go ahead and do the rest of the problem. I get an error that says that there is no appropriate constructor for the Job class, and i get errors in my declaration of the operator>> fuction that says it cant access the members of Job, but I think its because it says theres no constructor. anybody know why that is?
    Code:
    #include<iostream.h>
    #include<string.h>
    
    class Job
    {
    	friend istream& operator>>(istream& in, const Job &jobA);
    	friend ostream& operator<<(ostream& out, const Job &jobA);
    	private:
    		int JobNum;
    		int hours;
    		double rate;
    	public:
    		Job(const int JobNum, const int hours, const double rate);
    
    };
    istream& operator>>(istream& in, Job &jobA)
    {
    	cout<<endl;
    	cout<<"Enter a job number: ";
    	in>>jobA.JobNum;
    	cout<<"Hours to complete: ";
    	in>>jobA.hours;
    	cout<<"Rate per hour: ";
    	in>>jobA.rate;
    	return(in);
    }
    ostream& operator<<(ostream& out, const Job &jobA)
    {
    	out<<"Job #"<<jobA.JobNum<<" takes "<<jobA.hours<<" hours at $"<<jobA.rate<<" per hour."<<endl;
    		return(out);
    }
    int main()
    
    {
    
      Job jobA;  // instantiates an object, jobA
    
      cin>>jobA; //interactive input for data means no explicit 
    
     // constructor needed (page 289)
    
      cout<<jobA; // output pretty data members  (page 288)
    
    // The following line is for part c, using overload + and - operators
    
    //  cout<<"The difference between "<<jobA<<" and "<<jobB<<" is 
    
    // "<<(jobA - jobB)<<" hours"<<endl; (page 278)
    
    }

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    You are having constructor problems because you don't have a default one with no paramaters defined but when you create a Job object in main you do so without parameters. Either do one of two things 1) create a default constructor in your class like so:
    Code:
    public:
         Job(const int JobNum, const int hours, const double rate);
         Job() {};
    Note: if you hadn't created the overloaded constructor, then the class would automatically have created a default one on its own.

    2) AND/OR when you create you job object, give it parameters like so:
    Code:
    Job jobA(1, 8, 15.00);
    This should solve part of the problem you are having.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    why can't i use the members of the class as part of jobA?

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    why can the members be accessed int his program below, but not the one i posted earlier. they look to be the same structure to me.


    Code:
    #include<iostream.h>
    #include<string.h>
    
    class Meal
    {
    	friend istream& operator>>(istream& in, Meal &aMeal);
    	friend ostream& operator<<(ostream& out, Meal &aMeal);
    	private:
    		char mealName[10];
    		int calories;
    	public:
    		Meal(const char meal[], const int cal);
    		Meal operator+(const Meal transaction);
    };
    Meal::Meal(const char meal[], const int cal)
    {
    	strcpy(mealName, meal);
    	calories=cal;
    }
    Meal Meal::operator +(const Meal transaction)
    {
    	Meal temp("Daily Total",0);
    	temp.calories=calories+transaction.calories;
    	return(temp);
    }
    istream& operator>>(istream& in, Meal &aMeal)
    {
    	cout<<"Enter entree name: ";
    	in>>aMeal.mealName;
    	cout<<"Calories from meal: ";
    	in>>aMeal.calories;
    	return(in);
    }
    ostream& operator<<(ostream& out, Meal &aMeal)
    {
    	out<<"The "<<aMeal.mealName<<" contains "<<aMeal.calories<<" calories."<<endl;
    	return(out);
    }
    void main()
    {
    
     Meal breakfast("",0), lunch("",0), dinner("",0), total("Daily Total",0);
    
      cout<<"Breakast: ";
    
      cin>>breakfast;
    
      cout<<"Lunch: ";
    
      cin>>lunch;
    
      cout<<"Dinner: ";
    
      cin>>dinner;
    
      total = breakfast + lunch + dinner;
    
      cout<<breakfast<<endl;
    
      cout<<lunch<<endl;
    
      cout<<dinner<<endl;
    
      cout<<"---------------------"<<endl;
    
      cout<<total<<endl;
    
    }

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Are you using Microsoft Visual C++ to compile? If so, then there is a known bug where for some stupid reason friend functions don't work and are unable to access private members. If not, then can you post the error you are getting? And does the second code you provided compile or did you just copy it from somewhere?

  6. #6
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    yes, im using microsoft c++ version 6 from visual studio 6.0. the second program is from another problem in the book i have. i made it and compiled it and it works fine, but the first one i still have problems with. the errors i get are:



    error C2248: 'JobNum' : cannot access private member declared in class 'Job'
    see declaration of 'JobNum'
    error C2248: 'hours' : cannot access private member declared in class 'Job'
    declaration of 'hours'
    error C2248: 'rate' : cannot access private member declared in class 'Job'
    see declaration of 'rate'

  7. #7
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Okay, it looks like if you use the old header files (iostream.h) then you don't get the friend bug I was talking about. Still this header is depravated and you should use #include<iostream> and #include<string> instead.

    BUT in this case if you do that you will get the bug I was talking about. As for your error, the reason you are getting it is because you define your friend function as
    Code:
    friend istream& operator>>(istream& in, const Job &jobA);
    but in your implementation you define it as
    Code:
    istream& operator>>(istream& in, Job &jobA);
    WITHOUT the const before Job. So the compiler thinks this is a different function than the friend, and therefore doesn't have rights to the private members.

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    45
    oh, wow! i didn't even see that! thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 10:27 AM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM
  5. Overloading operators...
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 07:24 PM

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