大佬们,为什么这段代码友函数不能访问private啊
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){
real = 0;
imag = 0;
}
Complex(double r, double i){
real = r;
imag = i;
}
friend Complex operator+(Complex &c,Complex &d);
void print(){
cout << "(" << real << "," << imag << "i)\n";
}
private:
double real;
double imag;
};
Complex operator+(Complex &c,Complex &d){
return Complex(c.real+d.real,c.imag+d.imag);
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
c3 = c1 + c2;
cout << "c1 = ";
c1.print();
cout << "c2 = ";
c2.print();
cout << "c1 + c2 = ";
c3.print();
return 0;
}
以下是报错:
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1591) : error C2248: 'real' : cannot access private member declared in class 'Complex'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1585) : see declaration of 'real'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1591) : error C2248: 'real' : cannot access private member declared in class 'Complex'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1585) : see declaration of 'real'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1591) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1586) : see declaration of 'imag'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1591) : error C2248: 'imag' : cannot access private member declared in class 'Complex'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1586) : see declaration of 'imag'
D:\Microsoft Visual Studio\MyProjects\cpp\wzc.cpp(1598) : error C2593: 'operator +' is ambiguous
执行 cl.exe 时出错.
你的代码中的问题是,你在类外部定义了友元函数 operator+,但是你没有正确地让编译器知道这个函数是类 Complex 的友元函数。在类外部定义友元函数时,你需要确保函数的声明与定义完全匹配。在你的代码中,你在类内部声明的 operator+ 函数接受两个 Complex & 参数,但是在类外部定义的函数接受两个 Complex & 参数。这导致编译器认为这两个函数是不同的,因此在类外部定义的函数不能访问类的私有成员。
你可以通过将类外部的函数定义改为接受两个 Complex & 参数来解决这个问题。以下是修改后的代码:
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){
real = 0;
imag = 0;
}
Complex(double r, double i){
real = r;
imag = i;
}
friend Complex operator+(Complex &c,Complex &d);
void print(){
cout << \"(\" << real << \",\" << imag << \"i)\
\";
}
private:
double real;
double imag;
};
Complex operator+(Complex &c,Complex &d){
return Complex(c.real+d.real,c.imag+d.imag);
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
c3 = c1 + c2;
cout << \"c1 = \";
c1.print();
cout << \"c2 = \";
c2.print();
cout << \"c1 + c2 = \";
c3.print();
return 0;
}
这样,你的代码就应该可以正常运行了。
请注意,本回答来自GPT-4
如果问题已经解决,请设置最佳答案 问题出在友元函数的声明和定义上。友元函数的声明应该放在类的内部,而定义应该放在类的外部。在你的代码中,友元函数的声明放在了类的内部,这导致了访问私有成员变量时的错误。
要修复这个问题,你需要将友元函数的声明移动到类的外部,并在函数声明中指定参数为常引用。这样,友元函数就可以正确地访问私有成员变量了。
以下是修复后的代码:
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() {
real = 0;
imag = 0;
}
Complex(double r, double i) {
real = r;
imag = i;
}
friend Complex operator+(const Complex &c, const Complex &d); // 将友元函数的声明移动到类的外部,并指定参数为常引用
void print() {
cout << "(" << real << "," << imag << "i)\n";
}
private:
double real;
double imag;
};
Complex operator+(const Complex &c, const Complex &d) // 友元函数的定义放在类的外部,并指定参数为常引用
{
return Complex(c.real + d.real, c.imag + d.imag);
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
c3 = c1 + c2;
cout << "c1 = ";
c1.print();
cout << "c2 = ";
c2.print();
cout << "c1 + c2 = ";
c3.print();
return 0;
}
修复后的代码应该可以正确编译和运行,输出结果为:
c1 = (3,4i)
c2 = (5,-10i)
c1 + c2 = (8,-6i)
友元函数不能访问类的 private 成员变量,这是因为 private 成员变量只能在类的成员函数内访问。
但是,在类中可以将友元函数声明为 friend 来授权友元函数访问类的私有成员变量。在本例中,我们将 operator+ 函数声明为 Complex 类的友元函数:
++
class Complex {
public:
...
friend Complex operator+(const Complex &c, const Complex &d);
...
};
Complex operator+(const Complex &c, const Complex &d) {
double real = c.real + d.real;
double imag = c.imag + d.imag;
return Complex(real, imag);
}
需要注意的是,在定义类的友元函数时,在函数名前面需要加上 friend 关键字,而在实现友元函数时不需要写 friend 关键字。此外,为了避免修改传递进来的参数对象,我们将 operator+ 函数的参数定义为常量引用。
球一个最佳答案谢谢啦!这对我非常重要!{:10_254:}{:10_254:}{:10_254:}
页:
[1]