下面是我的代码。我有 3 个朋友功能:
#include <iostream>
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
friend Matrix operator++(Matrix&);
friend Matrix operator--(Matrix&);
friend ostream& operator<<(ostream&, Matrix&);
private:
int dim;
int** mat;
public:
Matrix(int d);
void set(int, int, int);
int get(int, int);
Matrix operator+(Matrix&);
Matrix operator-(Matrix&);
Matrix operator*(Matrix&);
~Matrix();
};
Matrix operator++(Matrix &m)
{
for (int i = 0; i < m.dim; i++)
{
for (int j = 0; j < m.dim; j++)
{
int val = m.get(i,j);
m.set(i,j,++val);
}
}
return m;
}
Matrix operator--(Matrix &m)
{
for (int i = 0; i < m.dim; i++)
{
for (int j = 0; j < m.dim; j++)
{
int val = m.get(i,j);
m.set(i,j,--val);
}
}
return m;
}
ostream& operator<<(ostream &os, Matrix &m)
{
for (int i = 0; i < m.dim; i++)
{
for (int j = 0; j < m.dim; j++)
{
os << m.get(i,j) << ' ';
}
os<<endl;
}
return os;
}
#endif
他们中的任何一个都没有抱怨“m.get(i,j)”,我认为这是一个公共功能......它抱怨“m.dim”,说它在运算符<<中不可访问,但不是在前两个。为什么??