C++ 中 abs() 的错误 [英] Error with abs() in c++
问题描述
在此代码的第 35 行使用 abs() 函数出错.我选择的编译器:c++(4.3.2)
Getting error with abs() function at line 35 in this code. Compiler I choosed : 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: In function '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:99: note: candidates are: double std::abs(double) /usr/include/c++/4.3/cmath:103: note: float std::abs(float)
/usr/include/c++/4.3/cmath:107: 注意:long double std::abs(long double)
/usr/include/c++/4.3/cmath:107: note: 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:99: note: candidates are: double std::abs(double)
/usr/include/c++/4.3/cmath:103: 注意:float std::abs(float)
/usr/include/c++/4.3/cmath:103: note: float std::abs(float)
/usr/include/c++/4.3/cmath:107: 注意:long double std::abs(long double)
/usr/include/c++/4.3/cmath:107: note: long double std::abs(long double)
可能是什么原因?
推荐答案
错误的原因可能是你没有包含头文件
.
The reason for the error can be that you did not include header <cstdlib>
.
#include <cstdlib>
标准 C 函数
int abs(int j);
在C头文件
中声明.
尽管 C++ 标准允许将标准 C 名称放在全局命名空间中,但最好使用限定名称,例如
Though the C++ Standard allows to place standard C names in the global namespace nevertheless it is better to use qualified names as for example
int dist = std::abs(pixelpos.first - i) + std::abs(pixelpos.second -j);
这篇关于C++ 中 abs() 的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!