【问题标题】:Error "subscript of pointer to function type 'int *(int)'"错误“指向函数类型'int *(int)'的指针的下标”
【发布时间】:2020-08-19 22:15:21
【问题描述】:

我正在编写代码来查找集群,我正在使用“cern root”来绘制图形, 数据保存在“.root”文件中,但代码是用 C++ 编写的。数据保存为 2D 直方图。代码的逻辑是一旦我找到一个包含一些信号的 bin,我找到它周围的邻居(8 个 bin),然后我标记 bin 并增加集群大小,然后对邻居做同样的事情。我开始虚构寻找邻居(该函数返回一个带有 x 坐标的数组,另一个找到 y 坐标)

int* neighbour_function_i(int i){   
int* neighbour_i = new int[8];   // Pointer to int, initialize to nothing.
neighbour_i[0] = {i-1}, neighbour_i[1] = {i}, neighbour_i[2] = {i+1}, neighbour_i[3] = {i-1}, neighbour_i[4] = {i+1}, neighbour_i[5] = {i-1}, neighbour_i[6] = {i}, neighbour_i[7] = {i+1};
return neighbour_i; //check if this works
}

找到集群的代码如下

    int* temp_neighbour_i = NULL;
    int* temp_neightbour_j = NULL;
    int uncheckedneighbours, total_neighbours;
    int clsize = 0;
    int temp_i,temp_j;

  for(int i = 0; i < NPIXAX; i++){
    for(int j = 0; j < NPIXAY; j++){
        clsize = 0;
        if(h->GetBinContent(i + 1, j + 1) - ped[i][j] > 0 && pedbf[i][j] == 0){//condition to find a cluster
           pedbf[i][j] = 1; //Tag arry
           clsize = 1;
           uncheckedneighbours = 8;
           total_neighbours = uncheckedneighbours;
           int* neighbour_i = neighbour_function_i[i];//the error is here
           int* neighbour_j = neighbour_function_j[j];//the error is here
           while(uncheckedneighbours != 0){
              for(int n = 0; n < total_neighbours; n++){
                temp_i = neighbour_i[n];//Temp int for coordienate 
                temp_j = neighbour_j[n];//Temp int for coordinate
                if(h->GetBinContent(temp_i, temp_j) - ped[temp_i][temp_j] > 0 && pedbf[temp_i][temp_j] == 0){//condition to find a cluster
                    pedbf[temp_i][temp_j] = 1;
                    int* new_neighbour_i = neighbour_function_i[temp_i];//the error is here
                    int* new_neighbour_j = neighbour_function_j[temp_j];//the error is here
                    uncheckedneighbours += 8;
                    total_neighbours += 8; 
                    int* temp_neighbour_i = new int[clsize * 8];
                    int* temp_neighbour_j = new int[clsize * 8];
                    clsize++;
                    temp_neighbour_i[n] = neighbour_i[n];//moving data to chnage the size of neighbour/i array
                    temp_neighbour_j[n] = neighbour_j[n];//moving data to change the size of neighbour_j array
                    delete[] neighbour_i;//deallocate neighbour
                    delete[] neighbour_j;//deallocate neighbour
                    int *neighbour_i = new int[clsize * 8]; //re-allocate the size of neighbour with size = size(clsize *8)
                    int *neighbour_j = new int[clsize * 8]; //re-allocate the size of neighbour with size = size(clsize *8)
                    for(int x = 0; x < (clsize - 1) * 8; x++){ //neighbour = temp_neighbour + new_neighbour
                        neighbour_i[x] = temp_neighbour_i[x]; 
                        neighbour_j[x] = temp_neighbour_j[x]; 
                    }
                    for(int x = (clsize - 1)*8; x < clsize * 8; x++){
                        neighbour_i[x] = new_neighbour_i[x];
                        neighbour_j[x] = new_neighbour_j[x];
                    }
                    delete[]temp_neighbour_i; //dealocate temp and new
                    delete[]temp_neighbour_j; //dealocate temp and new
                    delete[]new_neighbour_i; //dealocate temp and new
                    delete[]new_neighbour_j; //dealocate temp and new
                }
                uncheckedneighbours--;
              }
           }
        //if(clsize != 0){;//output to file cluseter size, i, j
        //}
        }
    }
 }

我不知道为什么我会收到这个错误“指向函数类型'int *(int)'的指针的下标”?

【问题讨论】:

  • std::array&lt;int, 8&gt; neighbour_function_i(int i){ return {{i-1, i, i+1, i-1, i+1, i-1, i, i+1}};} 怎么样?并摆脱(手动)分配?

标签: c++ root-framework


【解决方案1】:

也许问题应该作为拼写错误关闭,但函数会像这样调用:

int* neighbour_i = neighbour_function_i(i);

不是这样的:

int* neighbour_i = neighbour_function_i[i];

【讨论】:

  • @Tman 我敢肯定,这不是您的代码示例中的唯一问题。它会编译,但我看到很多问题,可能会导致运行时错误
  • 正如@Jarod42 建议的那样,使用 c++ std 容器,例如 std::array 或 std::vector。那你就不用#T手动管理内存了
  • 感谢您的cmets,如果您能指出问题,我将不胜感激
  • 代码示例太大且难以确定确切的错误。我将开始重构代码,用 std::array 或 std::vector 交换旧的 c 样式数组 []。
猜你喜欢
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多