【问题标题】:c++ subscript operator for 2D matrix between different files不同文件之间二维矩阵的c ++下标运算符
【发布时间】:2025-12-10 18:35:01
【问题描述】:

我创建了一个二维矩阵的模板类。 但是下标操作符(LINE 13,14,16~36)不能正常工作。

如果我标记 LINE 72~74(dio.cpp),程序可以运行。 LINE 91~93 可以工作。

如果 LINE 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 "..\..\include\mtrx.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  //==============================================================================

【问题讨论】:

    标签: c++ class templates operator-keyword subscript


    【解决方案1】:

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

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

    【讨论】:

    • 代理对象不值得为了使用 [x][y] 而不是 (x,y)。
    • @totowtwo:下标运算符是订阅数组或类数组对象的“标准”方式。接口很重要,特别是在通用上下文中工作时。
    • 下标运算符对于如何索引矩阵有最好的名称,但在实践中很少使用。 boost::ublas::matrixboost::gil::imageopencv::Mat 都使用 operator(int,int) 而不是代理对象。我相信boost::multiarray 专注于operator[int],但这具有多维背景。
    【解决方案2】:

    inn_input_stat 是一个指针。使用(*inn_input_stat)(x,y)。裸指针没有定义 operator(int,int),因此编译器尝试将其解释为函数。

    【讨论】:

    • 非常感谢!!! (*inn_input_stat)(x,y) 可以工作。答案很容易理解。