c++ - 好友功能无法访问的成员

标签 c++

我收到一个我无法解释的错误。这是我的头文件:

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

这里是我的定义:

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

它给我的错误是 Matrix.h 成员不可访问,但我明确指出它们是友元函数。

最佳答案

这些函数定义位于何处? friend 声明将名称注入(inject)到 namespace project 中。如果函数未在该命名空间中定义,则它们是不同的函数而不是 friend 。

关于c++ - 好友功能无法访问的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20643758/

相关文章:

c++ - 对类的 vector 进行排序

c# - 如何以编程方式访问操作系统中最常用的程序和程序的最新文件?

c++ - 排序链接列表 - 移动节点或交换数据成员?

c++ - 安全删除 StackView 转换中使用的 QML 组件

c++ - boost::any 如何检查空值/未定义值

c++ - 一旦失去焦点如何更新屏幕

c++ - Qt - 动态访问元素

c++ - 读取图像的像素值

c++ - 没有显式定义的构造函数时,为什么可以创建对象?

c++ - 高度图的正常平均