c++ - 使用模板友元函数 C++ 无效使用非静态数据成员错误

标签 c++ templates file-io multidimensional-array friend

#include <cassert>
#include <iostream>
#include <iostream>
#include <fstream>
#include <string> 
#include <iomanip>

using namespace std;

template <class T> class Matrix;

template <class T>
class Matrix
{
 public:

  friend istream& operator>>(istream& inStream,Matrix& other) 
 {
     string line ;
    T x ;
    unsigned _rows = 0 , _cols = 0 ;

    while(inStream.good())
    {
        while(getline(inStream,line))
        {
            istringstream streamA(line) ;
            _cols = 0;
            while(streamA >> x)
            {
                matrix[_rows][_cols] = x ;
                _cols++ ;
            }
            _rows++ ;
        }
    }
    return inStream ;
 }

  Matrix(unsigned r, unsigned c);
  Matrix(const Matrix<T>& rhs);
  ~Matrix();
  const Matrix operator=(const Matrix& other);

 void output() const ;

  // Matrix mathematical operations: insert overloaded operator signatures

  // Access the individual elements of a matrix: insert overloaded operator signatures

  // Getters:
  unsigned getRows() const; // Return number of rows
  unsigned getCols() const; // Return number of columns

 private:
  Matrix();

  T ** matrix; // the matrix array
  unsigned rows; // # rows
  unsigned cols; // # columns 

上面是我的.h文件。我正在尝试使 >> 运算符重载。在“friend istream& operator>>(istream& inStream,Matrix& other)”函数中,我试图从文本文件加载数据,但我一直收到 invalid use of non-static data member 错误。

最佳答案

friend 函数中没有隐式的this,所以你不能只使用非静态数据成员。您必须使用适当的名称访问您的 Matrix 的成员,例如 other.matrix

关于c++ - 使用模板友元函数 C++ 无效使用非静态数据成员错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25837683/

相关文章:

c++ - 非STL函数对象回调使用

c++ - 根据其他模板参数指定返回类型

c++ - 尝试编译 C++ 项目时出现 "unresolved external symbol"错误

java - 在 Java 中解析文本文件

.net - 从 IUnknown 实现接口(interface)类时出现错误 c2259

c++ - dll 的环境变量与 exe 不同

c++ - 将正数和负数存储在不同的 vector 中

c++ - Boost.TTI 无法与 Clang 配合使用

python - 如何从 bash 中的 python 脚本的一组不同输入文件中输出多个文件

java - 使用多线程写入文件