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

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

问题描述

我收到以下错误,当我使用常数 NVAR 而不是数量。

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

constants.h:

extern const unsigned int nVar;

constants.cpp:

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

的main.cpp

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

编译器:

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

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

之前的预期')'','令牌

expected ')' before ',' token

之前的预期不合格的ID常量

expected unqualified-id before 'const'

推荐答案

我想这是因为编译器应该知​​道数组在编译时的大小,但在NVAR你的例子价值将被称为只在连接由于为extern时间

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天全站免登陆