M
mark page
Hi to all
I am a newcomer to C++ and I am working my way through the book "C++ Primer
Third Edition".
I have a problem with one example and I am given the error message
: error C2248: 'hours' : cannot access private member declared in class
'Time'
: error C2248: 'minutes' : cannot access private member declared in class
'Time'
The problem relates to a Class Time and within it I have defined a friend
function (that has overloaded the << operator) to access the private data
members.
When I program the function inline within the class definition everthing
works, However, when the function definition is defined outside the class I
have the above problem. I am using the Visual C++ compiler version 6.
here is the code and I have highlighted the offending function with
comments.
// mytime.h -- class definition for time class
// demonstrates an overloaded operator for the Addition member
// includes friend functions
#ifndef _MYTIME_H_
#define _MYTIME_H_
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time operator+(const Time & t) const; // overloading the + operator to add
objects
Time operator-(const Time & t) const; // overloading the - operator to
subtract objects
Time operator*(double n) const; // overloading the * operator to
multiply objects by a factor
friend Time operator*(double m, const Time & t)
{ return t * m;} // inline definiton
// ************ problem function *********************
friend ostream & operator<<(ostream & os, const Time & t);
};
#endif
// mytime.cpp -- Time class method definitions
#include "mytime.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time:perator +(const Time & t) const // overloading the + operator
to add objects
{
Time sum;
sum.minutes = minutes + t.minutes;
sum. hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time Time:perator-(const Time & t) const // overloading the - operator to
subtract objects
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time Time:perator*(double mult) const // overloading the * operator to
multiply objects by a factor
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
// ***** problem function *********************************
ostream & operator<<(ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
please help
mark
I am a newcomer to C++ and I am working my way through the book "C++ Primer
Third Edition".
I have a problem with one example and I am given the error message
: error C2248: 'hours' : cannot access private member declared in class
'Time'
: error C2248: 'minutes' : cannot access private member declared in class
'Time'
The problem relates to a Class Time and within it I have defined a friend
function (that has overloaded the << operator) to access the private data
members.
When I program the function inline within the class definition everthing
works, However, when the function definition is defined outside the class I
have the above problem. I am using the Visual C++ compiler version 6.
here is the code and I have highlighted the offending function with
comments.
// mytime.h -- class definition for time class
// demonstrates an overloaded operator for the Addition member
// includes friend functions
#ifndef _MYTIME_H_
#define _MYTIME_H_
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h = 0, int m = 0);
Time operator+(const Time & t) const; // overloading the + operator to add
objects
Time operator-(const Time & t) const; // overloading the - operator to
subtract objects
Time operator*(double n) const; // overloading the * operator to
multiply objects by a factor
friend Time operator*(double m, const Time & t)
{ return t * m;} // inline definiton
// ************ problem function *********************
friend ostream & operator<<(ostream & os, const Time & t);
};
#endif
// mytime.cpp -- Time class method definitions
#include "mytime.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time:perator +(const Time & t) const // overloading the + operator
to add objects
{
Time sum;
sum.minutes = minutes + t.minutes;
sum. hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
Time Time:perator-(const Time & t) const // overloading the - operator to
subtract objects
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time Time:perator*(double mult) const // overloading the * operator to
multiply objects by a factor
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
// ***** problem function *********************************
ostream & operator<<(ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
please help
mark