1
setwidth()
是类Box
的成员函数,它也是Sbox
类的朋友函数,它设置两个类的成员width
的值。作为另一个类的成员函数的朋友函数设置值
Sbox
类width
的值未正确设置/显示。
#include<iostream>
using namespace std;
class Sbox;
class Box{
private:
double width;
public:
friend void printwidth(Box box);
void setwidth(Sbox sbox, double wid);
};
class Sbox {
private:
double width;
public:
friend void Box::setwidth(Sbox sbox, double wid);
void printwidth() {
cout << "the width of small box: " << width; //this value is coming wrong
}
};
void Box::setwidth(Sbox sbox, double wid) {
width = wid;
sbox.width = wid;
}
void printwidth(Box box) {
cout << "width of the box: " << box.width << endl;
}
int main() {
Box box;
Sbox sbox;
box.setwidth(sbox, 10.77);
printwidth(box);
sbox.printwidth();
return 0;
}
它的工作,谢谢 – falco97