如何启用友元类的友元功能直接在C++中访问其私有成员
How to enable a friend class's friend function access its private members directly in C++
我正在编写一个稀疏矩阵类,我想通过重载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。
相关文章:
- C++模板来检查友元函数的存在
- 模板类无法识别友元运算符
- 如何使用单独文件中的派生类访问友元函数对象
- 模板化的类和友元函数
- 为什么创建友元类的实例会导致"undefined reference to"错误?
- 如何在嵌套类中正确使用友元声明?
- 友元函数无法访问私有数据成员 (c++)
- 继承和友元函数,从基类访问受保护的成员
- 将子类方法声明为基类的友元
- 如何在友元函数中使用静态成员而不添加前缀 [类名]::
- 在将函数声明为友元时,尖括号的含义是什么?
- 在模板类之外定义友元函数的正确方法是什么?
- 2个模板化类的非模板友元函数未定义引用错误
- 友元方法作为常量
- C++ 友元函数和私有构造函数
- 使第二个类的构造函数成为第一个类中的友元函数
- 在为嵌套类定义行外友元时,我真的必须打破封装吗?
- 未定义的类模板不会实例化以检查友元函数
- C++类中的友元函数有问题?
- 使用typedef'ed返回类型声明友元函数时出现编译器错误