C++ C++;-我可以对多个文件同时使用extern和const吗?

C++ C++;-我可以对多个文件同时使用extern和const吗?,c++,visual-c++,C++,Visual C++,问题是,我试图为所有.h和.cpp文件设置一个全局常量变量,但当我这样做时,我得到了错误: array bound is not an integer constant before ‘]’ token 我不理解这一点,因为Z是一个常数。当我只使用一个文件执行此操作时,它会工作。我做错了什么 Number.h #include <iostream> extern const int Z; #include <iostream> #include "Number.h

问题是,我试图为所有
.h
.cpp
文件设置一个全局常量变量,但当我这样做时,我得到了错误:

array bound is not an integer constant before ‘]’ token
我不理解这一点,因为
Z
是一个常数。当我只使用一个文件执行此操作时,它会工作。我做错了什么


Number.h

#include <iostream>

extern const int Z;
#include <iostream>
#include "Number.h"

class b {
public:
    void Algo(double[Z][Z]);
};
#include <iostream>
#include "Number.h"

class c {
public:
    void Imprimir(double H[Z][Z]);
};
#包括
外部常数intz;

a.cpp

#include <iostream>
#include "b.h"
#include "c.h"
#include "Number.h"
using namespace std;


int main() {
    const int Z = 5;
    b Objeto1;
    c Objeto2;
    double H[Z][Z];
    Objeto1.Algo(H);
    Objeto2.Imprimir(H);
    return 0;
}
#include <iostream>
#include "b.h"
#include "Number.h"
using namespace std;

void b::Algo(double H[Z][Z]) {
    for(int a = 0; a < Z; a++) {
        for(int b = 0; b < Z; b++) {
            H[a][b] = Z;
            cout << H[a][b] << endl;
        }
    }
}
#include <iostream>
#include "c.h"
#include "Number.h"

using namespace std;
void c::Imprimir(double V[Z][Z]) {
    cout << "dfs" << endl;
}
#包括
#包括“b.h”
#包括“c.h”
#包括“Number.h”
使用名称空间std;
int main(){
常数int Z=5;
b目标1;
c目标2;
双H[Z][Z];
目的1.算法(H);
目的2.改进(H);
返回0;
}

b.h

#include <iostream>

extern const int Z;
#include <iostream>
#include "Number.h"

class b {
public:
    void Algo(double[Z][Z]);
};
#include <iostream>
#include "Number.h"

class c {
public:
    void Imprimir(double H[Z][Z]);
};
#包括
#包括“Number.h”
b类{
公众:
void算法(双[Z][Z]);
};

b.cpp

#include <iostream>
#include "b.h"
#include "c.h"
#include "Number.h"
using namespace std;


int main() {
    const int Z = 5;
    b Objeto1;
    c Objeto2;
    double H[Z][Z];
    Objeto1.Algo(H);
    Objeto2.Imprimir(H);
    return 0;
}
#include <iostream>
#include "b.h"
#include "Number.h"
using namespace std;

void b::Algo(double H[Z][Z]) {
    for(int a = 0; a < Z; a++) {
        for(int b = 0; b < Z; b++) {
            H[a][b] = Z;
            cout << H[a][b] << endl;
        }
    }
}
#include <iostream>
#include "c.h"
#include "Number.h"

using namespace std;
void c::Imprimir(double V[Z][Z]) {
    cout << "dfs" << endl;
}
#包括
#包括“b.h”
#包括“Number.h”
使用名称空间std;
void b::Algo(双H[Z][Z]){
对于(int a=0;a
非常好。但是,不能使用
Z
定义数组。因此,在下一行以及类似的其他行中使用
Z
是不正确的

class b{
public:
        void Algo(double[Z][Z]);

};
在编译时必须知道数组的大小。对于您提供的
extern
声明,这是不正确的

只有当您希望在运行时定义值,并且期望值在程序结束之前不会更改时,才有理由使用
extern const

如果只想将其用作定义数组的标记,请删除
extern
,并设置其值。使用:

const int Z = 5;
使用

非常好。但是,不能使用
Z
定义数组。因此,在下一行以及类似的其他行中使用
Z
是不正确的

class b{
public:
        void Algo(double[Z][Z]);

};
在编译时必须知道数组的大小。对于您提供的
extern
声明,这是不正确的

只有当您希望在运行时定义值,并且期望值在程序结束之前不会更改时,才有理由使用
extern const

如果只想将其用作定义数组的标记,请删除
extern
,并设置其值。使用:

const int Z = 5;

您可以有同时是
extern
const
的声明,其中一个关键字指定链接,另一个关键字是限定符,并且两者都可以组合。但是您从未定义过这样的变量。无论如何,不在全局范围内。还有一个问题是
const
变量不必是编译时常量variables,这是数组大小所必需的。您可能应该改为使用。错误消息具有误导性,应该说“Z不是整数常量表达式”。限定变量
常量的值可以是常量表达式,也可以不是常量表达式。您可以拥有同时是
外部
常量
的声明,其中一个关键字指定链接,另一个关键字是限定符,并且两者可以组合。但您从未定义此类变量。无论如何,不在全局范围内。然后
const
变量不必是编译时常量变量,这是数组大小所必需的。您可能应该改为使用。错误消息具有误导性,应该说“Z不是整数常量表达式”.const
限定变量的值可能是常量表达式,也可能不是常量表达式。非常感谢您的帮助!但是,例如,如果我想为所有文件定义const,我必须做什么?因为如果我删除外部变量,我会得到一个错误,即我正在重新定义constant@Mohammet,如果你再发一个问题会更好,使用合适的,这说明了问题。非常感谢您的帮助!但是,例如,如果我想为所有文件定义常量,我必须做什么?因为如果我删除外部,我会得到一个错误,即我正在重新定义constant@Mohammet,如果您发布另一个问题,并附上一个合适的答案,以证明您的专业问题。