简体   繁体   English

错误:“无穷大”没有命名类型

[英]error: ‘infinity’ does not name a type

I have a program which contain many cpp files,I try to create a makefile but when I run it I got some errors related to this function:我有一个包含许多 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;
        }
    }
}

} I tried to include :我试图包括:

#include <limits> 

using namespace std;

But when I compile my program I get these errors:但是当我编译我的程序时,我收到了这些错误:

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

I make a lot of research on the net but I didn't get any solution to fix my problem.我在网上做了很多研究,但没有找到任何解决方案来解决我的问题。

It seems likely that you are not compiling with C++11 or higher, since your compiler is complaining about the following line:您似乎没有使用 C++11 或更高版本进行编译,因为您的编译器抱怨以下行:

const auto infinity = std::numeric_limits<int>::infinity();

Assuming you have indeed included #include <limits> , there is nothing wrong with that line except the use of auto .假设您确实包含了#include <limits> ,那么除了使用auto之外,该行没有任何问题。 Without C++11, the compiler doesn't know what auto is.没有 C++11,编译器不知道auto是什么。 Compile with C++11 or higher, or change auto to int .使用 C++11 或更高版本编译,或将auto更改为int

Unrelated, and this was pointed out in the comments, but using numeric_limits<int>::infinity is an awful, awful way of checking things.不相关,这在评论中已经指出,但是使用numeric_limits<int>::infinity是一种非常糟糕的检查方式。 It doesn't make any sense to make an int comparison in regards to infinity.对无穷大进行int比较没有任何意义。 Prefer to usenumeric_limits<int>::max instead (or whatever else suits your purposes).更喜欢使用numeric_limits<int>::max (或其他任何适合您的目的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM