不同文件间二维矩阵的c++下标运算符

c++ subscript operator for 2D matrix between different files

本文关键字:c++ 运算符 下标 二维 文件      更新时间:2023-10-16

我创建了一个2D矩阵的模板类。但下标运算符(LINE13,14,16~36(不能正常工作。

若我标记LINE72~74(dio.cpp(,程序就可以运行了。线路91~93可以工作。

如果线路72~74(dio.cpp(没有标记。复杂故障和IDE引发错误"无功能调用"(BCB5 E2314(

需要你的帮助!

感谢

BR

Evyyos

001  //[mtrx.h]======================================================================
002  class mtrx
003  {
004     private:
005           unsigned int _row;
006           unsigned int _col;
007           T** _m;
008     public:
009           mtrx();
010           mtrx(unsigned int, unsigned int);
011           ~mtrx();
012  ... ...
013           T& operator()(unsigned int row, unsigned int col);
014           T operator()(unsigned int row, unsigned int col) const;
015  ... ...
016  //c++ subscript operator
017  template<class T> inline T&
018  mtrx<T>::operator () (unsigned int row, unsigned int col)
019  {
020     if((row < this->_row) && (col < this->_col)){
021        return this->_m[row][col];
022     }
023  }
024  
025  //c++ subscript operator
026  template<class T> inline T
027  //mtrx<T>::operator () (int row, int col) const
028  mtrx<T>::operator () (unsigned int row, unsigned int col) const
029  {
030     //if((row < this->_row) && (col < this->_col)){
031     //   return this->_m[row][col];
032     //}
033     if((row < _row) && (col < _col)){
034        return _m[row][col];
035     }
036  }
037  //==============================================================================
038  
039  //[dio.h]======================================================================
040  ... ...
041  #include "....includemtrx.h"
042  ... ...
043  unsigned long
044     dio_csv_analysis(
045           const char *ifile_name,
046           double smapling_ratio,
047           char fs,
048           unsigned int nn_selected_mask_size,
049           vector <int> inn_input_mask,
050           vector <int> inn_target_mask,
051           mtrx<double> *inn_input_stat,
052           mtrx<double> *inn_target_stat
053  );
054  ... ...
055  //==============================================================================
056  
057  //[dio.cpp]====================================================================
058  ... ...
059  unsigned long
060     dio_csv_analysis(
061           const char *ifile_name,
062           double smapling_ratio,
063           char fs,
064           unsigned int nn_selected_mask_size,
065           vector <int> inn_input_mask,
066           vector <int> inn_target_mask,
067           mtrx<double> *inn_input_stat,
068           mtrx<double> *inn_target_stat
069     )
070  {
071  ... ...
072              inn_input_stat(nn_input_mask_id, NN_INPUT_AVG) = f_item; <<Compile ERR:: all of nonfunction
073              inn_input_stat(nn_input_mask_id, NN_INPUT_MAX) = f_item; <<Compile ERR:: all of nonfunction
074              inn_input_stat(nn_input_mask_id, NN_INPUT_MIN) = f_item; <<Compile ERR:: all of nonfunction
075  
076  ... ...
077  //==============================================================================
078  
079  //[global.h]======================================================================
080  ... ...
081  #include "dio_csv.h"
082  ... ...
083  //==============================================================================
084  
085  //[main.cpp]====================================================================
086  ... ...
087     mtrx<double> nn_input_stat(nn_input_mask_size, 3);
088     mtrx<double> nn_target_stat(nn_target_mask_size, 3);
089  ... ...
090     double m=9,n=5,k=0;
091     nn_input_stat(0,0) = m; <<RUN TIME:: can work normally
092     nn_target_stat(0,0) = n; <<RUN TIME:: can work normally
093     n = nn_target_stat(0,0) - nn_input_stat(0,0); <<RUN TIME:: can work normally
094  ... ...
095  row_count=
096     dio_csv_analysis(
097           dio_csv_file.c_str(),
098           sampling_ratio,
099           ',',
100           nn_selected_mask_size,
101           nn_input_mask,
102           nn_target_mask,
103           &nn_input_stat,
104           &nn_target_stat
105     );
106  ... ...
107  //==============================================================================

mtrx::operator ()不是下标运算符,是函数调用运算符,调用方式如下:m(x, y)

下标运算符为mtrx::operator []。如果你需要一个二维下标运算符,你需要让第一个下标返回一个代理对象,这个代理对象也实现了下标运算符。当代理被下标时,评估调用所需的所有参数都准备好了。

inn_input_stat是一个指针。使用CCD_ 5。裸指针不定义运算符(int,int(,因此编译器试图将其解释为函数。