C++ :: 右侧的非法令牌
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2561368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Illegal token on right side of ::
提问by Adam Haile
I have the following template declaration:
我有以下模板声明:
template <typename T>
void IterTable(int& rIdx,
std::vector<double>& rVarVector,
const std::vector<T>& aTable,
const T aValue,
T aLowerBound = -(std::numeric_limits<T>::max()), //illegal token on right side of '::' shows here
bool aLeftOpen = true) const;
Which throws the illegal token error as noted, on the line with "-(std::numeric_limits::max())". I got this code from some old linux source that I'm trying to compile on Windows. Any idea what the issue is?
如上所述,在“-(std::numeric_limits::max())”行上抛出非法标记错误。我从一些我试图在 Windows 上编译的旧 linux 源代码中获得了这段代码。知道问题是什么吗?
Edit:It also fails using min(), and the compiler output is:
编辑:使用 min() 也失败,编译器输出为:
Error 92 error C2589: '::' : illegal token on right side of '::' c:\projects\r&d\prepaydll\include\cfcdefault.h 216 PrepayDLL
Error 93 error C2059: syntax error : '::' c:\projects\r&d\prepaydll\include\cfcdefault.h 216 PrepayDLL
Line 216, is the line previously mentioned.
第 216 行,就是前面提到的那一行。
回答by GManNickG
My guess is that max
has been made a macro. This happens at some point inside windows.h
.
我的猜测是max
已经做了一个宏。这发生在内部的某个点windows.h
。
Define NOMINMAX
prior to including to stop windows.h
from doing that.
NOMINMAX
在包括之前定义以停止windows.h
这样做。
EDIT:
编辑:
I'm still confident this is your problem. (Not including <limits>
would result in a different error). Place #undef max
and #undef min
just before the function and try again. If that fixes it, I was correct, and your NOMINMAX
isn't being defined properly. (Add it as a project setting.)
我仍然相信这是你的问题。(不包括<limits>
将导致不同的错误)。将#undef max
和#undef min
放在函数之前,然后重试。如果那解决了它,我是对的,并且您的NOMINMAX
定义不正确。(将其添加为项目设置。)
You can also prevent macro expansion by: (std::numeric_limits<T>::max)()
.
您还可以防止宏展开的:(std::numeric_limits<T>::max)()
。
On a side note, why not do std::numeric_limits<T>::min()
instead of negating the max?
附带说明一下,为什么不做std::numeric_limits<T>::min()
而不是否定最大值?
回答by John Dibling
Looks like you need to:
看起来你需要:
#include <limits>
#include <limits>
回答by John Zwinck
I wrote a "test harness" with a trivial struct containing your method declaration (and nothing else), and #included <limits>
and <vector>
, and invoked (and thus instantiated) the method with T
being int
, and it compiled just fine, both on Visual Studio 2008 Express on Windows Vista and with GCC 4.2.4 on Linux 2.6.
我写了一个“测试工具”包含您的方法声明(没有别的)一个简单的结构,并执行#included<limits>
和<vector>
,并调用(因此实例化)的方法与T
是int
,和它的Visual Studio 2008 Express的编译就好了,无论是在 Windows Vista 上和在 Linux 2.6 上使用 GCC 4.2.4。
I suggest trying to build only a minimal amount of code with the "problem" in it, and if that actually does build, add back in the rest of your project until it breaks, then you'll know what caused it.
我建议尝试只构建最少量的带有“问题”的代码,如果确实构建了,请重新添加项目的其余部分,直到它中断,然后你就会知道是什么导致了它。