473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Friend function problem

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!!!
Jul 22 '05 #1
4 2399
"Gactimus" <ga******@xrs.net> wrote in message
news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .
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

Jul 22 '05 #2
"David White" <no@email.provided> wrote in
news:Cy******************@nasal.pacific.net.au:
"Gactimus" <ga******@xrs.net> wrote in message
news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .
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?


Yeah it was. I fixed the problem by deleting the "using namespace std" line
in the headerfile and replaced it with "using std::ostream".
Jul 22 '05 #3
"David White" <no@email.provided> wrote in message
news:Cy******************@nasal.pacific.net.au...
"Gactimus" <ga******@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.


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

Jul 22 '05 #4
"Gactimus" <ga******@xrs.net> wrote in message news:1101261553.GjujyReqCwFEwAYYiUSFRA@bubbanews.. .
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.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I posted a few days ago about the same problem but was not very clear. So here is my second take at it. Basically with GCC 3.3.2, I can't compile the example from the C++ FAQ Lite available...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
10
by: Piotr Wyderski | last post by:
Hello, is it possible to reuse a friend operator which is defined inside a class? I'd like to obtain the following behaviour: class integer { integer operator +(signed long int v) const...
4
by: Justin Miller | last post by:
Ok, I tried to make that subject as descriptive as possible. What I'm trying to do: I'm attempting to use policies to create a generic memento (design pattern) template. My Memento template so...
3
by: CoolPint | last post by:
After upgrading to gcc 3.4.2 from gcc 3.2.3, I got compiler errors that I could not figure out. After reading other postings, I learned that my coding was not compliant to the standard in the first...
6
by: Joseph Turian | last post by:
I am having difficulty defining a friend function because of a #include cycle: ============ obj.H ================ #ifndef _obj_ #define _obj_ #include "cont.H" class cont;
4
by: fdmfdmfdm | last post by:
I have the following code: #include <iostream> #include <cstdlib> #include <cassert> using namespace std; template <class T> class Stack{ public: enum{DefaultStack = 10, EmptyStack = -1};
4
by: ciccio | last post by:
Dear all, once again I stumbled upon the following puzzling problem. When having the following two files (see below), the gnu compiler compiles the file without a problem while the compiler...
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
9
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.