【发布时间】:2018-08-21 05:58:02
【问题描述】:
我写了下面这段代码
#include <iostream>
const int N = 5;
class X
{
public:
int array[N];
void foo()
{
std::cout << "array size:"<<sizeof(array)/N << std::endl;
}
enum
{
N = 3
};
};
int main()
{
X x;
x.foo();
}
上述代码不能用 GCC 编译:
<source>:13:8: error: declaration of 'N' [-fpermissive]
N = 3
^
<source>:2:11: error: changes meaning of 'N' from 'const int N' [-fpermissive]
const int N = 5;
^
从我的编译时间来看,数组被定义为一个由五个整数组成的数组,N 被定义为 5。编译器如何解析变量的名称声明?
【问题讨论】:
-
在 Clang 上编译,而不是在 GCC 上。您应该在问题中填写完整的错误消息。
-
在 MSVC 2018 上编译。
-
std::cout << "array size:"<<sizeof(array)/N << std::endl;-->std::cout << "array size:" << std::size(array) << std::endl;
标签: c++ variables scope declaration