[英]error: ‘infinity’ does not name a type
我有一個包含許多 cpp 文件的程序,我嘗試創建一個 makefile,但是當我運行它時,我遇到了一些與此功能相關的錯誤:
void replace_infinites(cv::Mat_<int>& matrix) {
const unsigned int rows = matrix.rows,columns = matrix.cols;
// assert( rows > 0 && columns > 0 );
if(rows==0 || columns==0)
return;
double max = matrix(0, 0);
const auto infinity = std::numeric_limits<int>::infinity();
// Find the greatest value in the matrix that isn't infinity.
for ( unsigned int row = 0 ; row < rows ; row++ ) {
for ( unsigned int col = 0 ; col < columns ; col++ ) {
if ( matrix(row, col) != infinity ) {
if ( max == infinity ) {
max = matrix(row, col);
} else {
max = std::max<int>(max, matrix(row, col));
}
}
}
}
// a value higher than the maximum value present in the matrix.
if ( max == infinity ) {
// This case only occurs when all values are infinite.
max = 0;
} else {
max++;
}
for ( unsigned int row = 0 ; row < rows ; row++ ) {
for ( unsigned int col = 0 ; col < columns ; col++ ) {
if ( matrix(row, col) == infinity ) {
matrix(row, col) = max;
}
}
}
我試圖包括:
#include <limits>
using namespace std;
但是當我編譯我的程序時,我收到了這些錯誤:
munkres.cpp: In function ‘void replace_infinites(cv::Mat_<int>&)’:
munkres.cpp:44:16: error: ‘infinity’ does not name a type
munkres.cpp:49:38: error: ‘infinity’ was not declared in this scope
munkres.cpp:60:17: error: ‘infinity’ was not declared in this scope
munkres.cpp:69:38: error: ‘infinity’ was not declared in this scope
我在網上做了很多研究,但沒有找到任何解決方案來解決我的問題。
您似乎沒有使用 C++11 或更高版本進行編譯,因為您的編譯器抱怨以下行:
const auto infinity = std::numeric_limits<int>::infinity();
假設您確實包含了#include <limits>
,那么除了使用auto
之外,該行沒有任何問題。 沒有 C++11,編譯器不知道auto
是什么。 使用 C++11 或更高版本編譯,或將auto
更改為int
。
不相關,這在評論中已經指出,但是使用numeric_limits<int>::infinity
是一種非常糟糕的檢查方式。 對無窮大進行int
比較沒有任何意義。 更喜歡使用numeric_limits<int>::max
(或其他任何適合您的目的)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.