注册 登录
编程论坛 C++教室

一个友元声明的错误,找不出来啊,求帮忙

江帆 发布于 2014-07-24 11:08, 490 次点击
#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;
}
3 回复
#2
江帆2014-07-24 11:11
就是我把count函数都声明为两个类的友元,但是定义count时只能访问第二个类的私有数据,但是无法访问第一个类的私有数据,也就是最后  cout << a.general + b.general << endl;这条语句中a.general是错的,但是b.general却是对的
#3
韶志2014-07-25 08:52
const shang_stock &a
你这算什么
#4
rjsp2014-07-25 10:13
程序代码:

#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_ 成员变量的值。
1