首页 文章

c中的abs()出错

提问于
浏览
1

在此代码中第35行的abs()函数出错 . 我选择的编译器:c(4.3.2)

在底部看错误 .

void bfs(pair<int,int> pixelpos){
bfsq.push(pixelpos);
int u,v,i,j;
pair<int,int> tmpq;
while(!bfsq.empty()){
    tmpq = bfsq.front();
    u = tmpq.first; v = tmpq.second;
    bfsq.pop();
    r(i,u-square_dist,u+square_dist) r(j,v-square_dist,v+square_dist)
      if(inrange(i,j)){
      // ERROR HERE DOWN IN abs() fn
        int dist = abs(pixelpos.first - i) + abs(pixelpos.second -j); // Line: 35
        if(graph[i][j]>dist){
            graph[i][j] = dist;
            bfsq.push(pair<int,int> (i,j));
          }
      }
}

prog.cpp:在函数'void bfs(std :: pair)'中:

prog.cpp:35: error: call of overloaded 'abs(int)' is ambiguous

/ usr / include / c /4.3/cmath:99:注意:候选人是:double std :: abs(double)/ usr / include / c /4.3/cmath:103:注意:float std :: abs(float)

/ usr / include / c /4.3/cmath:107:注意:long double std :: abs(long double)

prog.cpp:35: error: call of overloaded 'abs(int)' is ambiguous

/ usr / include / c /4.3/cmath:99:注意:候选人是:double std :: abs(double)

/ usr / include / c /4.3/cmath:103:注意:float std :: abs(float)

/ usr / include / c /4.3/cmath:107:注意:long double std :: abs(long double)

What might be the reason?

1 回答

  • 3

    出错的原因可能是您没有包含 Headers <cstdlib> .

    #include <cstdlib>
    

    标准C功能

    int abs(int j);
    

    在C头 <stdlib.h> 中声明 .

    虽然C标准允许在全局命名空间中放置标准C名称,但最好使用限定名称作为示例

    int dist = std::abs(pixelpos.first - i) + std::abs(pixelpos.second -j);
    

相关问题