【发布时间】:2018-05-12 12:34:20
【问题描述】:
这个问题似乎只出现在 MacOS 上,在 linux 上也可以使用 clang 编译。
以下代码是一个简化,但演示了问题,
#include<iostream>
int index = 0;
int main()
{
std::cout << index << std::endl;
}
在编译时抛出此错误:
main.cpp:2:5: error: redefinition of 'index' as different kind of symbol
int index = 0;
^
/usr/include/strings.h:73:7: note: previous definition is here
char *index(const char *, int) __POSIX_C_DEPRECATED(200112L);
^
main.cpp:5:18: warning: address of function 'index' will always evaluate to
'true' [-Wpointer-bool-conversion]
std::cout << index << std::endl;
~~ ^~~~~
main.cpp:5:18: note: prefix with the address-of operator to silence this warning
std::cout << index << std::endl;
^
&
1 warning and 1 error generated.
这些是使用的编译器参数:
clang++ -std=c++11 main.cpp -o test
当使用 stdio 删除 iostream 时,代码按预期编译。他们是解决这个问题的方法还是我必须重命名我的变量以避免这种情况?
我确实找到了this,但我已经在使用 C++11 标志,而且 -std=c11 标志似乎对 C++ 代码无效。
【问题讨论】:
-
你为什么期望它编译?
-
你认为
index是什么? -
不小心弄错了索引应该是名称而不是类型。
-
您显然是在重新输入代码 - 不要那样做 - 使用复制和粘贴来发布它。
-
代码和错误信息不匹配。请从示例代码中发布真正的错误。