为什么声明了友元函数还是不能访问私有变量?
#include <iostream>using namespace std;
//____qcodep____
//声明活期储蓄类
class CurrentSavings
{
private:
int balance;
public:
CurrentSavings(int balance) {
//____qcodep____
this->balance = balance;
}
//____qcodep____
friend void printTotalSavings(TimeSavings& ts, CurrentSavings& cs);
int print() {
cout << this->balance << endl;
}
};
//声明定期储蓄类
class TimeSavings
{
private:
int balance;
public:
TimeSavings(int balance) {
//____qcodep____
this->balance = balance;
}
//____qcodep____
friend void printTotalSavings(TimeSavings& ts, CurrentSavings& cs);
int print() {
cout << this->balance << endl;
}
};
void printTotalSavings(TimeSavings& ts, CurrentSavings& cs)
{
//____qcodep____
cout << ts.balance + cs.balance << endl;
}
int main(void)
{
int cs, ts;
cin >> cs >> ts;
CurrentSavings cs1(cs);
TimeSavings ts1(ts);
cs1.print();
ts1.print();
printTotalSavings(ts1, cs1);
return 0;
}
这段代码已经在class TimeSavings和class CurrentSavings都声明了友元函数,但是为什么在cout << ts.balance + cs.balance << endl;中只有ts.balance可以访问,但是cs.balance却无法访问呢?
在C++中,友元函数可以访问类的私有成员变量。但是,你的代码存在一些问题。
首先,在你的代码中,声明了两次相同的友元函数 `printTotalSavings(TimeSavings& ts, CurrentSavings& cs)`,这是不必要的。你只需要在其中一个类中声明该友元函数即可。
其次,在你的 `print()` 函数中,有两处错误:
1. 在第一个 `print()` 函数中,`cout` 语句的格式有误。应该是 `cout << "balance = " << balance;`。
2. 第二个 `print()` 函数中,你使用了未定义且未声明的变量 `ts`,这会导致编译错误。
修复上述错误后的代码示例如下:
#include <iostream>
using namespace std;
// 声明定期储蓄类
class TimeSavings;
// 声明活期储蓄类
class CurrentSavings
{
private:
int balance;
public:
CurrentSavings(int balance) {
this->balance = balance;
}
friend void printTotalSavings(TimeSavings& ts, CurrentSavings& cs);
int print() {
cout << "balance = " << balance << endl;
return 0;
}
};
// 声明定期储蓄类
class TimeSavings
{
private:
int balance;
public:
TimeSavings(int balance) {
this->balance = balance;
}
friend void printTotalSavings(TimeSavings& ts, CurrentSavings& cs);
int print() {
cout << "balance = " << balance << endl;
return 0;
}
};
// 定义友元函数
void printTotalSavings(TimeSavings& ts, CurrentSavings& cs) {
int total = ts.balance + cs.balance;
cout << "Total savings = " << total << endl;
}
int main() {
CurrentSavings cs(1000);
TimeSavings ts(2000);
cs.print();
ts.print();
printTotalSavings(ts, cs);
return 0;
}
现在你可以成功访问 `CurrentSavings` 和 `TimeSavings` 类的私有变量,并输出预期的结果。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 #include <iostream>
using namespace std;
class TimeSavings;
//____qcodep____
//声明活期储蓄类
class CurrentSavings
{
private:
int balance;
public:
CurrentSavings(int balance) {
//____qcodep____
this->balance = balance;
}
//____qcodep____
friend void printTotalSavings(TimeSavings& ts, CurrentSavings& cs);
int print() {
cout << this->balance << endl;
// return ?;
return 0;
}
};
//声明定期储蓄类
class TimeSavings
{
private:
int balance;
public:
TimeSavings(int balance) {
//____qcodep____
this->balance = balance;
}
//____qcodep____
friend void printTotalSavings(TimeSavings& ts, CurrentSavings& cs);
int print() {
cout << this->balance << endl;
// int print
return -1;
}
};
void printTotalSavings(TimeSavings& ts, CurrentSavings& cs)
{
//____qcodep____
cout << ts.balance + cs.balance << endl;
}
int main(void)
{
int cs, ts;
cin >> cs >> ts;
CurrentSavings cs1(cs);
TimeSavings ts1(ts);
cs1.print();
ts1.print();
printTotalSavings(ts1, cs1);
return 0;
}
页:
[1]