| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 490 人关注过本帖
标题:一个友元声明的错误,找不出来啊,求帮忙
只看楼主 加入收藏
江帆
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-7-21
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:3 
一个友元声明的错误,找不出来啊,求帮忙
#include<iostream>
#include<string>
using namespace std;
class shen_stock{
    int general, ST, PT;
public:
    shen_stock(int general = 0, int ST=0, int PT = 0);
    void print(const shen_stock &a );
    void addgen();
    void addst();
    void addpt();
    friend void shang_stock::print(const shang_stock &a);
    friend void count(const shen_stock&a, const shang_stock&b);


};
shen_stock::shen_stock(int g, int s, int p){
    general = g;
    ST = s;
    PT = p;
}
void shen_stock::addgen(){ ++general; }
void shen_stock::print(const shen_stock &a){
    cout << a.general << endl << a.ST << endl << a.PT << endl;
   
}


class shang_stock{
    int general, ST, PT;
public:
    shang_stock(int general = 0, int ST = 0, int PT = 0);
    void print(const shang_stock &a);
    void addgen();
    void addst();
    void addpt();
    friend void shen_stock::print(const shen_stock &a);
    friend void count(const shen_stock&a, const shang_stock&b);
   



};
shang_stock::shang_stock(int g, int s, int p){
    general = g;
    ST = s;
    PT = p;
}
void shang_stock::addgen(){ ++general; }
void shang_stock::print(const shang_stock &a){
    cout <<a.general << endl << a.ST << endl << a.PT << endl;

}

void count(const shen_stock&a, const shang_stock&b){

    cout << a.general + b.general << endl;

}


int main(){



    return 0;
}
搜索更多相关主题的帖子: public general include friend count 
2014-07-24 11:08
江帆
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2014-7-21
收藏
得分:0 
就是我把count函数都声明为两个类的友元,但是定义count时只能访问第二个类的私有数据,但是无法访问第一个类的私有数据,也就是最后  cout << a.general + b.general << endl;这条语句中a.general是错的,但是b.general却是对的
2014-07-24 11:11
韶志
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:斗气大陆
等 级:贵宾
威 望:44
帖 子:2223
专家分:13592
注 册:2013-3-22
收藏
得分:5 
const shang_stock &a
你这算什么

三十年河东,三十年河西,莫欺少年穷!
2014-07-25 08:52
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
程序代码:
#include <iostream>

////////////////// shen_stock //////////////////

class shang_stock;

class shen_stock
{
public:
    explicit shen_stock( int general=0, int ST=0, int PT=0 );
    shen_stock& operator++();
    int operator+( const shang_stock& rhs ) const;
private:
    int general_, ST_, PT_;

    friend class shang_stock;
    friend std::ostream& operator<<( std::ostream& os, const shen_stock& obj );
};

shen_stock::shen_stock( int general, int ST, int PT ) : general_(general), ST_(ST), PT_(PT)
{
}

shen_stock& shen_stock::operator++()
{
    ++general_;
    return *this;
}

std::ostream& operator<<( std::ostream& os, const shen_stock& obj )
{
    return os << '(' << obj.general_ << ',' << obj.ST_ << ',' << obj.PT_ << ')';
}

////////////////// shang_stock //////////////////

class shen_stock;

class shang_stock
{
public:
    explicit shang_stock( int general=0, int ST=0, int PT=0 );
    shang_stock& operator++();
    int operator+( const shen_stock& rhs ) const;
private:
    int general_, ST_, PT_;

    friend class shen_stock;
    friend std::ostream& operator<<( std::ostream& os, const shang_stock& obj );
};

shang_stock::shang_stock( int general, int ST, int PT ) : general_(general), ST_(ST), PT_(PT)
{
}

shang_stock& shang_stock::operator++()
{
    ++general_;
    return *this;
}

std::ostream& operator<<( std::ostream& os, const shang_stock& obj )
{
    return os << '(' << obj.general_ << ',' << obj.ST_ << ',' << obj.PT_ << ')';
}

////////////////// shen_stock & shang_stock //////////////////

int shen_stock::operator+( const shang_stock& rhs ) const
{
    return general_ + rhs.general_;
}

int shang_stock::operator+( const shen_stock& rhs ) const
{
    return general_ + rhs.general_;
}

////////////////// test code //////////////////

using namespace std;

int main()
{
    shen_stock a(1);
    cout << a << endl;

    shang_stock b(2);
    cout << b << endl;

    cout << (a+b) << endl;

    return 0;
}

当然,最好的办法不是用友元,而是增加一个 int general() const 成员函数用来返回 general_ 成员变量的值。
2014-07-25 10:13
快速回复:一个友元声明的错误,找不出来啊,求帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012451 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved