'numeric_limits' 不是 'std' 的成员。

8

我试图从源代码编译一个包括sol2库的应用程序,FlyWithLua

我遵循说明进行操作,但当我运行cmake --build ./build时,出现以下错误:

In file included from /home/jon/src/FlyWithLua/src/FloatingWindows

/FLWIntegration.cpp:10:
/home/jon/src/FlyWithLua/src/third_party/sol2/./upstream/sol.hpp: In lambda function:
/home/jon/src/FlyWithLua/src/third_party/sol2/./upstream/sol.hpp:7194:59: 
      error: ‘numeric_limits’ is not a member of ‘std’
7194 |               std::size_t space = (std::numeric_limits<std::size_t>::max)();

这行代码之后还有几个其他错误,但是我猜如果我能解决这一个,其他的问题可能就会消失。

针对类似问题,解决方案是在.hpp文件中添加以下include语句。

类似问题有很多,需要添加这些include语句。

#include <stdexcept>
#include <limits>

sol.hpp文件中包含以下导入:

#include <stddef.h>
#include <limits.h>

https://sol2.readthedocs.io/en/latest/errors.html提供了一些关于编译器无法识别这些包含文件的原因的提示:

Compiler Errors / Warnings

A myriad of compiler errors can occur when something goes wrong. Here is some basic advice about working with these types:

If there are a myriad of errors relating to std::index_sequence, type traits, 
and other std:: members, it is likely you have not turned on your C++14 switch for
your compiler. Visual Studio 2015 turns these on by default, but g++ and clang++ 
do not have them as defaults and you should pass the flag --std=c++1y or
--std=c++14, or similar for your compiler.

src/CMakeList.txt文件包含以下行:

set(CMAKE_CXX_STANDARD 17)

我对C/C++只有一点了解,这看起来非常复杂,但我希望对于更有经验的人来说,可能存在一个容易识别的原因和解决方案。

cat /etc/*-release命令给出:

DISTRIB_RELEASE=21.10
DISTRIB_CODENAME=impish
DISTRIB_DESCRIPTION="Ubuntu 21.10"

$ g++ --version
g++ (Ubuntu 11.2.0-7ubuntu2) 11.2.0

3
我有点困惑。你是否有 #include <limits>?那应该可以解决问题。 - G.M.
我有#include <limits.h>,但没有#include <limits>。 - CHsurfer
4
@CHsurfer limitslimits.h 是两个不同的头文件,你需要前者。limits.h 是 C 标准库的一个头文件。 - user17732522
2
关于 "I have #include <limits.h> but not #include <limits>":请按建议操作,加入 #include <limits>limits.hC 语言头文件,不了解诸如 std 的命名空间。 - G.M.
我在sol.hpp中添加了#include <limits>,编译成功了。谢谢! - CHsurfer
显示剩余2条评论
2个回答

11

我通过在使用该语句的相应文件中仅包含limits而不是limits.h来消除了这个错误。

#include <limits>

10
/home/jon/src/FlyWithLua/src/third_party/sol2/./upstream/sol.hpp:7194:59:

      error: ‘numeric_limits’ is not a member of ‘std’
7194 |               std::size_t space = (std::numeric_limits<std::size_t>::max)();
这个错误信息意味着 src/third_party/sol2/./upstream/sol.hpp头文件使用了std::numeric_limits,但std::numeric_limits没有被定义。最简单的解释是定义std::numeric_limits的头文件未被包含。在这种情况下,解决方案是包含定义std::numeric_limits的头文件。

the sol.hpp file includes the following imports:

#include <stddef.h>
#include <limits.h>

这证实了问题。这些标头都没有定义 std::numeric_limits

https://sol2.readthedocs.io/en/latest/errors.html 提供了一些关于为什么编译器可能无法识别这些包含文件的提示:

这些提示可能适用于其他情况,但不适用于此情况。 std::numeric_limits 自从 C++ 标准问世以来就一直是其一部分,因此语言版本对其存在没有影响。


结论:根据引用的错误消息,sol.hpp 使用位于标头<limits> 中定义的 std::numeric_limits ,但根据您说它没有包含该标头。如果是这种情况,则 sol.hpp 文件存在错误。正确的解决方法是在使用std::numeric_limits之前在该文件中包含<limits>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接