c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'

代码如下:

#ifndef _DATE_H_
#define _DATE_H_
#include<iostream>
using namespace std; class Date
{
public:
Date();
Date(int y,int m,int d);
void printOn();
private:
int _year;
int _mounth;
int _day; friend const ostream& operator<<(ostream & out,Date d);
};
Date::Date():_year(0),_mounth(0),_day(0)
{}
Date::Date(int y,int m,int d):_year(y),_mounth(m),_day(d)
{
}
void Date::printOn()
{
cout<<*this; } const ostream& operator<<(ostream & out,Date d)
{
out<<d._year<<d._mounth<<d._day<<endl;
return out;
}
#endif

错误提示如下图:

据说是VC的一个经典BUG。和namespace也有关.

只要含有using namespace std; 就会提示友员函数没有访问私有成员的权限。

解决方法:去掉using namespace std;换成更小的名字空间。

例如:

含有#include <string>就要加上using std::string

含有#include <fstream>就要加上using std::fstream

含有#include <iostream>就要加上using std::cin; using std::cout; using std::ostream; using std::istream; using std::endl; 等等,需要什么即可通过using声明什么.

更正后如下:

#ifndef _DATE_H_
#define _DATE_H_
#include<iostream> //using namespace std;
using std::cin;
using std::endl;
using std::cout;
using std::ostream;
using std::istream;
class Date
{
public:
Date();
Date(int y,int m,int d);
void printOn();
private:
int _year;
int _mounth;
int _day; friend const ostream& operator<<(ostream & out,Date d);
};
Date::Date():_year(0),_mounth(0),_day(0)
{}
Date::Date(int y,int m,int d):_year(y),_mounth(m),_day(d)
{
}
void Date::printOn()
{
cout<<*this; }
const ostream& operator<<(ostream & out,Date d)
{
out<<d._year<<d._mounth<<d._day<<endl;
return out;
}
#endif

或者更改如下:

#include<iostream.h>

[置顶] c++,vc6.0,中友元函数,无法访问私有字段(private)的问题(problem),cannot access private member declared in class 'Date'的更多相关文章

  1. VC6.0中友元函数无法访问类私有成员的解决办法

    举个例子: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #inclu ...

  2. [置顶] Hadoop2.2.0中HDFS的高可用性实现原理

    在Hadoop2.0.0之前,NameNode(NN)在HDFS集群中存在单点故障(single point of failure),每一个集群中存在一个NameNode,如果NN所在的机器出现了故障 ...

  3. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  4. 在VC6.0中能不能使用Duilib界面库呢?

    Duilib库的源代码是在vs2010下编译的,一般适用于vs2008及以上的版本开发使用,那么duilib能不能在vc6.0的工程中使用呢?如何在vc6.0中使用duilib库呢? 今天,由于工作要 ...

  5. thinkphp中的内置操作数据库与mysql中的函数汇总

    8.4.4 Model类getModelName() 获取当前Model的名称getTableName() 获取当前Model的数据表名称switchModel(type,vars=array()) ...

  6. VC6.0中添加库文件和头文件

    附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tools>Options>Directories>Include files. 对于特定项目的头文件包含,在“ ...

  7. VC6.0中重载操作符函数无法访问类的私有成员

    整理日: 2015年03月18日 在 C++ 中,操作符(运算符)可以被重载以改写其实际操作.同时我们可以定义一个函数为类的朋友函数(friend function)以便使得这个函数能够访问类的私有成 ...

  8. VC6.0中MFC界面换肤简例

    利用VC中的MFC进行界面设计时,发现界面上的各控件无法简易地进行调整,比如字体大小.颜色.格式等. 为了改变外观,小小地美化一下,今天决定动手一试. 网上提供的库和方法不计其数,我选择了SkinMa ...

  9. gcc的bug? c++模板类中友元函数的訪问权限问题

    原文地址:http://stackoverflow.com/q/23171337/3309790 在c++中,模板类中能够直接定义一个友元函数.该函数拥有訪问该模板类非public成员的权限. 比方: ...

随机推荐

  1. HDU4738 Caocao's Bridges 无向图的桥

    一眼题:找所有的桥,然后求最小权值 但是有很多坑点 1:如果本来不联通 输出0,(这个坑我知道) 2:但是还有一个坑,就是当整个连通,最小桥的权值是0时,也必须派一个人去,wa了无数遍(还是太年轻) ...

  2. 《Nagios系统监控实践》一书出版

    本书是我的第一本译著,有此机会实属机缘巧合.虽然使用Nagios只有一年多的时间,但是作为用户,我深感其设计的简洁与高效—没有一丝多余的东西.因为工作的关系,要求对各个领域都有所了解,所以没有仔细地阅 ...

  3. JS 框架之我感

    对于一些js框架在我看来,都是将前端的表现动态化,即用动态js把html加载到页面上,如angularJS的MVVM开发模式(已接触),ReactJS的View层组件化(学习中),还有一些只听过没见过 ...

  4. C++ 模板类解析

    具体模板类作用这边就不细说了,下面主要是描述下模板类的使用方法以及注意的一些东西. #include <iostream> using namespace std; template &l ...

  5. 解决dwr报错【 Error: java.lang.SecurityException: No class by name: service】

    打开包含dwr的网页时后台报错: 警告: Names of known classes are: __System DwrQueryService 十二月 11, 2015 10:24:44 上午 o ...

  6. 通过在shell脚本中用scp或rsync实现远程同步文件

    通过在shell脚本中用expect实现远程scp文件  shell expect的简单用法 http://myunix.blog.51cto.com/191254/1095074 http://ji ...

  7. 读取.tmx地图

    读取.tmx地图 m_GameMap = CCTMXTiledMap::create("map1.tmx"); this->addChild(m_GameMap,1); 读取 ...

  8. CC_CALLBACK原理及应用

    http://my.oschina.net/u/555701/blog/219844 c++ 11 基础 :     std::function 类模版 std::function是一种通用.多态的函 ...

  9. C#- Winform调用BAT例子

    前段时间在工作的时候需要用到,百度了好久后找,可是找到了又希望调用的时候窗体不要显示出来. proc.StartInfo.CreateNoWindow = true;       proc.Start ...

  10. 分布式模式之broker模式

    转自:http://blog.chinaunix.net/uid-23093301-id-90459.html 问题来源: 创建一个游戏系统,其将运行在互联网的环境中.客户端通过WWW服务或特定的客户 ...