Friend function problem

G

Gactimus

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::eek:perator+(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::eek:perator-(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::eek:perator*(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!!!
 
D

David White

Gactimus said:
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;

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
 
G

Gactimus

Was this 'using' in the book?

Yeah it was. I fixed the problem by deleting the "using namespace std" line
in the headerfile and replaced it with "using std::eek:stream".
 
D

David White

David White said:
You didn't post the line numbers from the
error messages, so it's impossible to tell where the problem is.

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
 
L

Larry Brasfield

Gactimus said:
Hey again, Yo.

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:

I've trimmed it severely. Please note my code insertion below.
#include <iostream>
using namespace std;

class Time;
ostream & operator<<(ostream & os, const Time & t);
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!!!

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.
 
S

Scuro

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

Welcome into the club! :)
It's a visual C++ bug. Try this:

..h:
friend ostream& foo(ostream& os, const Time& t);
friend ostream& operator<<(ostream& os, const Time& t);

..cpp:
ostream& foo(ostream& os, const Time& t) { // Correct.
//code
}

ostream& operator<<(ostream& os, const Time& t) { // Error!!!!
//code
}

You'll get an error only if the name of the funcion is "operator<<".
Solutions to your problem:

1) Update visual c++ compiler
2) Use another compiler (i suggest GCC and dev-c++
(http://www.bloodshed.net/devcpp.html))
3) put the operator<< code in .h file instead of .cpp file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top