Connecting Tech Pros Worldwide Forums | Help | Site Map

Friend function problem

Gactimus
Guest
 
Posts: n/a
#1: Jul 22 '05
Hey again,

I am working my way through the book "C++ Primer Fourth Edition".

I am trying to run a program out of the book using Microsoft Visual C++
version 6 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'

Here is the code. I have indicated where the compiler says the problem lies:

//mytime3.h
#ifndef MYTIME0_H_
#define MYTIME0_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;
Time operator-(const Time & t) const;
Time operator*(double n) const;
friend Time operator*(double m, const Time & t)
{return t * m;}
friend ostream & operator<<(ostream & os, const Time & t);
};
#endif


// mytime3.cpp
#include "mytime3.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::operator+(const Time & t) const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}

Time Time::operator-(const Time & t) const
{
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::operator*(double mult) const
{
Time result;
long totalminutes = hours * mult * 60 + minutes * mult;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}

//THE PROBLEM IS HERE
ostream & operator<<(ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}

//usertime3.cpp
#include <iostream>
#include "mytime3.h"
using namespace std;

int main()
{
Time A;
Time B(5, 40);
Time C(2, 55);

cout << "A, B, and C:\n";
cout << A << "; " << B << "; " << endl;
A = B + C;
cout << "B + C: " << A << endl;
A = B * 2.75;
cout << "B * 2.75: " << A << endl;
cout << "10 * B: " << 10 * B << endl;
return 0;
}


Please help!!!

David White
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Friend function problem


"Gactimus" <gactimus@xrs.net> wrote in message
news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .[color=blue]
> Hey again,
>
> I am working my way through the book "C++ Primer Fourth Edition".
>
> I am trying to run a program out of the book using Microsoft Visual C++
> version 6 and I am given the error message:
>
> : error C2248: 'hours' : cannot access private member declared in class[/color]
'Time'[color=blue]
> : error C2248: 'minutes' : cannot access private member declared in class[/color]
'Time'[color=blue]
>
> Here is the code. I have indicated where the compiler says the problem[/color]
lies:[color=blue]
>
> //mytime3.h
> #ifndef MYTIME0_H_
> #define MYTIME0_H_
> #include <iostream>
> using namespace std;[/color]

Was this 'using' in the book? Very bad idea to put this in a header file,
because anything that #includes this file is forced to bring in the entire
std namespace.

[snip]

If I run the code as you posted it, except with the two #includes for
"mytime3.h" commented out (because I'm compiling all in a single file), I
get no error message in VC++ 6.0. You didn't post the line numbers from the
error messages, so it's impossible to tell where the problem is.

DW



Gactimus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Friend function problem


"David White" <no@email.provided> wrote in
news:CySod.2046$ob1.40784@nasal.pacific.net.au:
[color=blue]
> "Gactimus" <gactimus@xrs.net> wrote in message
> news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .
>[color=green]
>> Hey again,
>>
>> I am working my way through the book "C++ Primer Fourth Edition".
>>
>> I am trying to run a program out of the book using Microsoft Visual C++
>> version 6 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'
>>
>> Here is the code. I have indicated where the compiler says the problem
>> lies:
>>
>> //mytime3.h
>> #ifndef MYTIME0_H_
>> #define MYTIME0_H_
>> #include <iostream>
>> using namespace std;[/color]
>
> Was this 'using' in the book?[/color]

Yeah it was. I fixed the problem by deleting the "using namespace std" line
in the headerfile and replaced it with "using std::ostream".
David White
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Friend function problem


"David White" <no@email.provided> wrote in message
news:CySod.2046$ob1.40784@nasal.pacific.net.au...[color=blue]
> "Gactimus" <gactimus@xrs.net> wrote in message
> news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .
> You didn't post the line numbers from the
> error messages, so it's impossible to tell where the problem is.[/color]

Correction. I didn't notice where you'd pointed out the location in the
code. Anyway, I still don't get an error.

My guess is that you need a service pack for VC++ 6.0. Mine has SP5.

DW



Larry Brasfield
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Friend function problem


"Gactimus" <gactimus@xrs.net> wrote in message news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .[color=blue]
> Hey again,[/color]
Yo.
[color=blue]
> I am working my way through the book "C++ Primer Fourth Edition".
>
> I am trying to run a program out of the book using Microsoft Visual C++
> version 6 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'
>
> Here is the code. I have indicated where the compiler says the problem lies:[/color]

I've trimmed it severely. Please note my code insertion below.
[color=blue]
> #include <iostream>
> using namespace std;[/color]

class Time;
ostream & operator<<(ostream & os, const Time & t);
[color=blue]
> 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;
> Time operator-(const Time & t) const;
> Time operator*(double n) const;
> friend Time operator*(double m, const Time & t)
> {return t * m;}
> friend ostream & operator<<(ostream & os, const Time & t);
> };
> Please help!!![/color]

Your code compiles fine with a less buggy compiler.
Your difficulty is due to a (somewhat) well known bug
in MSVC6. If you insert the code I did above, the
function being declared as a friend will exist enough
in the symbol table to be befriendable and treated
properly later.

--
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me.


Closed Thread