Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Members inaccessible with friend function

Tags:

c++

I am getting an error that I cannot explain. Here is my header file:

#include <iostream>
using namespace std;
namespace project
{
#ifndef MATRIX_H
#define MATRIX_H

typedef int* IntArrayPtr;
class Matrix
{
public:
    friend ostream& operator<<(ostream& out, const Matrix& object);
    friend istream& operator>>(istream& in, Matrix& theArray);
    //Default Constructor
    Matrix();

    Matrix(int max_number_rows, int max_number_cols, int intial_value);

    //Destructor
    ~Matrix();
    //Copy Constructor
    Matrix(const Matrix& right_side);
    //Assignment Operator
    Matrix& operator=(const Matrix& right_side);

    void Clear();
    int Rows();
    int Columns();
    bool GetCell(int x,int y, int& val);
    bool SetCell(int x,int y, int val);
    //void Debug(ostream& out);
private:
    int initialVal;
    int rows;
    int cols;
    IntArrayPtr *m;
};
#endif
}

And here my definition:

ostream& operator<<(ostream& out, const Matrix& object)
{
    for(int r = 0; r < object.rows; r++)
    {
        for(int c = 0; c < object.cols; c++)
        {
            out << object.m[r][c] << " ";
        }
        out << endl;
    }
    return out;
}

It's giving me the error that Matrix.h members are inaccessible, but I clearly stated that they are friend function.

like image 943
user3112739 Avatar asked Sep 01 '25 10:09

user3112739


1 Answers

Where are those function definitions located? The friend declaration injects the names into namespace project. If the functions aren't defined in that namespace, they're different functions and not friends.

like image 194
Pete Becker Avatar answered Sep 03 '25 02:09

Pete Becker