簡體   English   中英

C++ 從友元函數(運算符<<)訪問私有數組的元素

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

我有一個帶有以下聲明和隨附定義的類:

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

我能夠很好地調用公共成員函數,並且我認為我能夠從朋友函數訪問私有數據成員。

關於為什么我收到此錯誤的任何想法?

注意:根據要求,以下是完整課程的更多內容:

首先是頭文件。

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);

如果我們應用第二個修復程序,這就是我們的文件現在的樣子。

保利.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;
};

保利.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;
}

主文件

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

using namespace std; 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM