【问题标题】:Error with abs() in c++c++ 中的 abs() 错误
【发布时间】:2016-05-24 13:21:29
【问题描述】:

此代码第 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: 错误:重载 'abs(int)' 的调用不明确

/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: 错误:重载 'abs(int)' 的调用不明确

/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)

可能是什么原因?

【问题讨论】:

  • "c++(4.3.2)" - 这不是编译器。你是说海合会吗?
  • @imvamshi 我认为原因是你没有包含标题
  • @ChristianHackl 是的。我忘了提。它的 GCC v4.3.2

标签: c++ overloading


【解决方案1】:

错误的原因可能是您没有包含标头&lt;cstdlib&gt;

#include <cstdlib>

标准 C 函数

int abs(int j);

在 C 头文件 &lt;stdlib.h&gt; 中声明。

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

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

【讨论】:

    猜你喜欢
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2015-06-17
    • 2011-11-28
    • 2020-11-28
    相关资源
    最近更新 更多