专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > C++

成员函数设成友元函数仍是不能访问私有成员

发布时间:2011-06-28 13:56:07 文章来源:www.iduyao.cn 采编人员:星星草
成员函数设成友元函数还是不能访问私有成员
看上去一堆乱七八糟的东西 其实就是一个简单的问题 prune是plate类的成员函数 要用node类的私有数据,所以在node类中将prune函数声明为友元 然后些prune函数的实现语句 发现任然没法访问所需要的node类的私有数据,求大神相助!

################
类定义
################
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<map>
#include<stdlib.h>//包含atof函数
#include<sstream>//为了转换double到string
//---------------------------------------------------------------
typedef std::vector<std::vector<std::string>> Raw_format;
//---------------------------------------------------------------
//定义node类
//---------------------------------------------------------------
class plate;
class Node
{
public:
    typedef std::vector<std::vector<double>> Node_format;
    void get_and_set(Raw_format&);
void display();
Node_format extract();
friend void Plate::prune(Node&);
private:
Node_format original,modified;
};
//-----------------------------------------------------------------
//定义plate类(element的一种)
//-----------------------------------------------------------------
class Plate
{
public:
typedef std::vector<std::vector<double>> Plate_format;
typedef std::map<int,std::vector<double>> Plate_set_format;
void get_and_set(Element::Element_format&);
void display();
Plate_set_format get_the_set();
Plate_format extract();
void prune(Node&);
private:
Plate_format original,modified;
};
################################
类实现(一部分)
################################
void Plate::prune(Node &n)
{
n.original//示意一下,这里我的编译器就提示有问题了,说不可访问
}




------解决方案--------------------
改变类定义的顺序
#include <iostream>
using namespace std;
#include<vector>
#include<string>
#include<fstream>
#include<iostream>
#include<map>
#include<stdlib.h>//
#include<sstream>//
//---------------------------------------------------------------
typedef std::vector<std::vector<std::string> > Raw_format;
//---------------------------------------------------------------

//---------------------------------------------------------------
class plate;
class Node;

class Plate
{
public:
    typedef std::vector<std::vector<double> > Plate_format;
    typedef std::map<int,std::vector<double> > Plate_set_format;
//    void get_and_set(Element::Element_format&);
//    void display();
//    Plate_set_format get_the_set();
//    Plate_format extract();
    void prune(Node&);
private:
    Plate_format original,modified;
};

class Node
{
public:
    typedef std::vector<std::vector<double> > Node_format;
//    void get_and_set(Raw_format&);
//    void display();
//    Node_format extract();
    friend void Plate::prune(Node&);
private:
    Node_format original,modified;
};

void Plate::prune(Node &n)
{
    n.original.empty();//
}
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}

------解决方案--------------------
class Plate 要在node之前定义

------解决方案--------------------
引用:
谢谢啊 但除此之外就没有别的方法了吗?


不明白,调换顺序又不复杂咯
还要别的什么方法啊?
被指为友元的类, 必须先定义的。

------解决方案--------------------
居然被人抢先了,哎
对,改变顺序性
但是还可以这样,把
friend void Plate::prune(Node &n);

改成
friend class Plate;

虽然这样有点改变你的原意,但是这就是规矩啊
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: