Search code examples
c++privatefriend

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


I've got a class with the following declaration and accompanying definition:

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

and

private:
    int *polyArray;

Inside the code for that operator function, I have (among other things):

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

I get the following error message from an underline in my compiler (Visual Studio) specifically underneath "polyArray":

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

I'm able to call public member functions just fine, and I thought I was able to access a private data member from a friend function.

Any ideas on why I'm getting this error?

NOTE: Below is more of the complete class, as requested:

First, the header file.

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

Then the implementation.

#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;
}

Finally, main.

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

using namespace std;

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

Solution

  • A directive to use a namespace (e.g. using namespace std) only applies to code and header files written after the directive.

    In your header file, Poly.h, there's no definition for ostream because ostream is in namespace std. There are three possible fixes:

    • using namespace std in the header file (bad practice)
    • using std::ostream in the header file
    • Use the full name of std::ostream

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

    If we apply the second fix, this is what our files look like now.

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