如何启用友元类的友元功能直接在C++中访问其私有成员

How to enable a friend class's friend function access its private members directly in C++

本文关键字:友元 C++ 访问 成员 功能 何启用 启用      更新时间:2023-10-16

我正在编写一个稀疏矩阵类,我想通过重载operator<<来输出稀疏矩阵。 我想知道如何启用SMatrix(operator<<(的友元功能直接(而不是通过某些接口(访问TriTuple的私人数据成员?请注意,SMatrix同时是TriTuple的友元类。请参阅代码,如下所示。

// tri-tuple term for sparse matrix by the form <row, col, value>
template<typename T>
class TriTuple {
template<typename U> friend class SMatrix;
// enable friend of SMatrix access private members of TriTuple
// declaring like this? feasible in VS2019, but not in gcc
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const SMatrix<U>& M);
private:
size_t _row, _col;
T _val;
public:
//...
};
// sparse matrix
template<typename T>
class SMatrix {
template<typename U>
friend std::ostream& operator<<(std::ostream& os, const SMatrix<U>& M);
private:
size_t _rows, _cols;// # of rows & columns
size_t _terms;      // # of terms
TriTuple<T>* _arr;  // stored by 1-dimensional array
size_t _maxSize;
public:
//...  
};
template<typename U>
std::ostream& operator<<(std::ostream& os, const SMatrix<U>& M)
{
M.printHeader();
for (size_t i = 0; i < M._terms; ++i) {
os << M._arr[i]._row << "tt" << M._arr[i]._col << "tt" << M._arr[i]._val << 'n';
}
return os;
}

它可以在VS2019(可能是C++17(中成功编译和运行,但无法在gcc中编译(目前只有c ++ 11可用(。 那个 c++ 标准版本的问题吗?(它指的是"ISO C++禁止声明......"(我应该如何改进声明? 请参阅下图中的错误消息。 海湾合作委员会error_msg 提前谢谢你们很棒的家伙:-(

[class.friend]/p11:

如果友元声明出现在本地类 ([class.local]( 中,并且指定的名称是非限定名称,则查找先前的声明时不考虑最内层封闭的非类作用域之外的作用域。对于友元函数声明,如果没有先验声明,则程序格式不正确。对于友元类声明,如果没有先前的声明,则指定的类属于最内层的封闭非类作用域,但如果随后引用它,则在最内层的封闭非类作用域中提供匹配的声明之前,名称查找不会找到它的名称。

您需要提供SMatrix的定义,或者在引用它之前至少向前声明它:

template <typename U>
class SMatrix;

此错误与类SMatrix的前向声明有关。 只需尝试转发声明

template<typename T>
class SMatrix;

TriTuple之上.

在神螺栓上检查它

也没有检查operator<<中的空矩阵。我建议你把-fsanitize=undefined -fsanitize=address用于 gcc。