我的C++友元函数为什么不能访问类的私有变量 25
//mytime2.h文件#include<iostream>usingnamespacestd;classTime{private:inthours;intminute...
//mytime2.h文件
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
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
//mytime2.h文件的实现文件
#include "mytime2.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
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;
}
ostream & operator << (ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
//使用类的主文件
#include <iostream>
#include "mytime2.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 << "; " << C << 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;
}
我用VC6.0编译的文件,下面这个函数出现了编译错误
ostream & operator << (ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
错误如下:
--------------------Configuration: mytime2 - Win32 Debug--------------------
Compiling...
mytime2.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\eight\mytime2\mytime2.cpp(79) : error C2248: 'hours' : cannot access private member declared in class 'Time'
c:\program files\microsoft visual studio\myprojects\eight\mytime2\mytime2.h(10) : see declaration of 'hours'
C:\Program Files\Microsoft Visual Studio\MyProjects\eight\mytime2\mytime2.cpp(79) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
c:\program files\microsoft visual studio\myprojects\eight\mytime2\mytime2.h(11) : see declaration of 'minutes'
Error executing cl.exe.
mytime2.exe - 2 error(s), 0 warning(s)
我这是照着C++ primer plus的书敲进去的,一字不差的,作为初学者练习来的,但却不能运行,很郁闷!希望各位高手能赐教! 展开
#include <iostream>
using namespace std;
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = 0);
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
//mytime2.h文件的实现文件
#include "mytime2.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
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;
}
ostream & operator << (ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
//使用类的主文件
#include <iostream>
#include "mytime2.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 << "; " << C << 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;
}
我用VC6.0编译的文件,下面这个函数出现了编译错误
ostream & operator << (ostream & os, const Time & t)
{
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
}
错误如下:
--------------------Configuration: mytime2 - Win32 Debug--------------------
Compiling...
mytime2.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\eight\mytime2\mytime2.cpp(79) : error C2248: 'hours' : cannot access private member declared in class 'Time'
c:\program files\microsoft visual studio\myprojects\eight\mytime2\mytime2.h(10) : see declaration of 'hours'
C:\Program Files\Microsoft Visual Studio\MyProjects\eight\mytime2\mytime2.cpp(79) : error C2248: 'minutes' : cannot access private member declared in class 'Time'
c:\program files\microsoft visual studio\myprojects\eight\mytime2\mytime2.h(11) : see declaration of 'minutes'
Error executing cl.exe.
mytime2.exe - 2 error(s), 0 warning(s)
我这是照着C++ primer plus的书敲进去的,一字不差的,作为初学者练习来的,但却不能运行,很郁闷!希望各位高手能赐教! 展开
展开全部
1. 将 #include <iostream> 改成 #include <iostream.h>
2. using namespace std; 删掉即可
刚编译通过了!
2. using namespace std; 删掉即可
刚编译通过了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是可以的,不如你试试其他的编译器,我在dev-cpp做是可以的!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你的vc6.0打上sp5补丁。
这是微软的bug。
这是微软的bug。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2006-11-01
展开全部
我也有过同样的疑问,期待....
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询