C++ 从友元函数(运算符 <<)访问私有数组的元素

C++ Accessing an element of a private array from a friend function (operator <<)

我有一个 class 包含以下声明和随附的定义:

friend ostream& operator<<(ostream& out, const Poly& poly);
ostream& operator<<(ostream& out, const Poly& poly) {}

private:
    int *polyArray;

在该运算符函数的代码中,我有(除其他外):

    if (poly.polyArray[i] != 0) {
        out << poly.polyArray[i];
    }

我从编译器 (Visual Studio) 的下划线中得到以下错误消息,特别是在 "polyArray" 下方:

int *Poly::polyArray 
Member "Poly::polyArray" is inaccessible

我可以很好地调用 public 成员函数,而且我认为我可以从友元函数访问私有数据成员。

关于我为什么会收到此错误的任何想法?

注意: 根据要求,以下是更多完整的 class:

首先是头文件。

class Poly
{
public:
    Poly(int coeff, int expon);
    friend ostream& operator<<(ostream& out, const Poly& poly);
private:
    int *polyArray;
    int size;
};

然后执行。

#include <iostream>
#include "Poly.h"

using namespace std;

Poly::Poly(int coeff, int expon) {
    size = expon + 1;
    polyArray = new int[size];
    for (int i = 0; i < size; ++i) {
        polyArray[i] = (i == expon ? coeff : 0);
    }
}

ostream& operator<<(ostream& out, const Poly& poly) {
    int currentSize = poly.getSize();
    // If Poly is "empty", print 0
    if (currentSize == 1 && poly.polyArray[0] == 0) {
        out << 0;
        return out;
    }
    for (int i = 0; i < currentSize; ++i) {
        // Print the "+" sign if the coefficient is positive
        //if (poly.polyArray[i] > 0) {
        if (poly.polyArray[i] > 0) {
            out << "+";
        }
        // Print the coefficient if it is not 0, skipping it and all following code otherwise
        if (poly.polyArray[i] != 0) {
            out << poly.polyArray[i];
        }
        else {
            continue;
        }
        // Print "x" if the exponent is greater than 0
        if (i > 0) {
            out << "x";
        }
        // Print exponent if it is greater than 1
        if (i > 1) {
            out << "^" << i;
        }
        // Print a space if this is not the last term in the polynomial
        if (i != currentSize - 1) {
            out << " ";
        }
    }
    return out;
}

最后,主要。

#include "Poly.h"
#include <iostream>

using namespace std;

int main() {
    Poly y(5, 7);
    cout << y;
    return 0;
}

使用命名空间的指令(例如using namespace std)仅适用于在该指令之后编写的代码和头文件。

在您的头文件 Poly.h 中,没有 ostream 的定义,因为 ostream 在名称空间 std 中。存在三种可能的修复方法:

  • using namespace std 在头文件中(不好的做法)
  • 头文件中
  • using std::ostream
  • 使用std::ostream的全名

    friend std::ostream& operator<<(std::ostream& out, const Poly& poly);

如果我们应用第二个修复,这就是我们的文件现在的样子。

Poly.h

#include <iostream>

using std::ostream; 

class Poly
{
public:
    Poly(int coeff, int expon);
    friend ostream& operator<<(ostream& out, const Poly& poly);
private:
    int *polyArray;
    int size;
};

Poly.cc

#include <iostream>
#include "Poly.h"
using namespace std;

Poly::Poly(int coeff, int expon) {
    size = expon + 1;
    polyArray = new int[size];
    for (int i = 0; i < size; ++i) {
        polyArray[i] = (i == expon ? coeff : 0);
    }
}

ostream& operator<<(ostream& out, const Poly& poly) {
    int currentSize = poly.getSize();
    // If Poly is "empty", print 0
    if (currentSize == 1 && poly.polyArray[0] == 0) {
        out << 0;
        return out;
    }
    for (int i = 0; i < currentSize; ++i) {
        // Print the "+" sign if the coefficient is positive
        //if (poly.polyArray[i] > 0) {
        if (poly.polyArray[i] > 0) {
            out << "+";
        }
        // Print the coefficient if it is not 0, skipping it and all following code otherwise
        if (poly.polyArray[i] != 0) {
            out << poly.polyArray[i];
        }
        else {
            continue;
        }
        // Print "x" if the exponent is greater than 0
        if (i > 0) {
            out << "x";
        }
        // Print exponent if it is greater than 1
        if (i > 1) {
            out << "^" << i;
        }
        // Print a space if this is not the last term in the polynomial
        if (i != currentSize - 1) {
            out << " ";
        }
    }
    return out;
}

main.cc

#include <iostream>
#include "Poly.h"

using namespace std; 

int main() {
    Poly y(5, 7);
    cout << y;
    return 0;
}