繁体   English   中英

包含时的奇怪行为<vector>

[英]Strange behavior when including <vector>

当我包含标准向量库时,我看到一些非常奇怪的行为。 谁能告诉我是什么导致这些故障? 我要踩谁的脚趾,我应该注意这个错误的一般情况吗?

我正在使用ROOT C ++解释器。 通过命令行解释器界面或编译后的代码,

$ root -l
root [0] #include <vector>
root [1] float max = -1.15;
root [2] if (max < -1 && max > -1.2) max = 2;
Error: Variable name has bad character 'max<-1&&max>-1207' (tmpfile):1:
Error: Symbol max<-1&&max>-1207 is not defined in current scope  (tmpfile):1:
Error: Variable name has bad character 'max<-1&&max>-1' (tmpfile):1:
Error: Symbol max is not defined in current scope  (tmpfile):1:
Error: non class,struct,union object $max<-1&&max>-1 used with . or -> (tmpfile):1:
*** Interpreter error recovered ***
root [3] max
(float)(-1.14999997615814209e+00)

然后,如果我添加一些不应该做的括号:

root [4] if ((max < -1) && (max > -1.2)) max = 2; 
root [5] max
(float)2.00000000000000000e+00
root [6] .qqqqqqqqqqqq

如果我只是正常退出root用户,则出现root seg-faults。 如果我不包括它,它应该工作:

[abarker@cmslpc29 macro]$ r
root [0] float max = -1.15;
root [1] if (max < -1 && max > -1.2) max = 2;
root [2] max
(float)2.00000000000000000e+00

另外,如果我将变量名更改为“ max”以外的名称,该问题将消失。

max由<vector>悄悄定义。

$ root -l
root [0] max
Error: Symbol max is not defined in current scope  (tmpfile):1:
*** Interpreter error recovered ***
root [1] max(1,2)
Error: Function max(1,2) is not defined in current scope  (tmpfile):1:
*** Interpreter error recovered ***
root [2] #include <vector>
root [3] max
(const void*)0x2b6758507c37
root [4] max(2,3)
(const int)3

因此,解释器中的一些错误使重载的名称绊倒,并弄乱了范围规则。

看起来解释器将max视为std::max ,这是一个模板。 这就是为什么它试图将<>的文本视为模板参数。 这是高度不合格的; max在命名空间中定义std ,所以叫变量冲突max

暂无
暂无

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

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