用常数初始化数组不起作用 [英] initialize array with constant number does not work

查看:32
本文介绍了用常数初始化数组不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用常量 nVar 而不是数字时,出现以下错误.

I get the following error when I use constant nVar instead of a number.

常量.h:

extern const unsigned int nVar;

constants.cpp:

#include "constants.h"
const unsigned int nVar = 5;

ma​​in.cpp

#pragma once
#include "constants.h"
void foo(const double q[nVar])
{
    // ...
}

编译器:

数组常量不是']'标记前的整数常量

array constant is not an integer constant before ']' token

在 ',' 标记之前应有 ')'

expected ')' before ',' token

'const' 之前的预期非限定 ID

expected unqualified-id before 'const'

推荐答案

我认为这是因为编译器应该在编译时知道数组的大小,但是在您的示例中,由于 extern,nVar 的值仅在链接时才知道

I think it happens because compiler should know array's size at compile time, but in your example value of nVar will be known only at linking time due to extern

这篇关于用常数初始化数组不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆