简体   繁体   English

C ++:使用多个文件中的const变量初始化数组大小时出错

[英]C++ : Error Initilializing Array Size with Const Variable in multiple files

I know that when declaring an array I have to specified its size with a constant value, but in this case I'm creating a const value that is also a const expression, initialized with a literal value, which can be evaluated in compiling time, but I keep having the error on those two cases: 我知道在声明数组时,我必须使用恒定值指定其大小,但是在这种情况下,我将创建一个const值,它也是一个const表达式,并使用文字值初始化,可以在编译时对其求值,但是在这两种情况下我仍然有错误:

CASE I: 情况一:

Stack.h 堆栈

#ifndef STACK_H
#define STACK_H

extern const unsigned MAX;

class Stack {

public:
  /* Declarations here ... */

private:
    unsigned n;
    int stack[MAX];

};

#endif // STACK_H

Stack.cpp Stack.cpp

#include <iostream>
#include "Stack.h"

extern const unsigned MAX = 5;

Stack::Stack() {
    this->n  = 0;
}

int Stack::pop() {
    int pop = -1;

    if (n > 0) {
        pop = this->stack[n - 1];
        this->stack[n - 1] = 0;
        --n;
    } else {
        std::cout << "StackUnderFlowException:: Stack Data Structure is Empty!" << std::endl;;
    }

    return pop;
}

int Stack::getStackTop() {
    return this->n > 0 ? this->stack[n - 1] : -1;
}

void Stack::push(int v) {
    if (n < MAX) {
        this->stack[n] = v;
        ++n;
    } else {
        std::cout << "StackOverFlowException:: Stack Data Structure is Full!" << std::endl;
    }
}

Error: 错误:

In file included from p38.cpp:2:
./Stack.h:18:6: error: fields must have a constant size: 'variable length array in structure' extension will never be supported
        int stack[MAX];
            ^
1 error generated.
In file included from Stack.cpp:2:
./Stack.h:18:6: error: fields must have a constant size: 'variable length array in structure' extension will never be supported
        int stack[MAX];
            ^

And things get even more weird in the second case ... 在第二种情况下,事情变得更加奇怪了...

CASE II: 案例二:

Stack.h

#ifndef STACK_H
#define STACK_H

extern const unsigned MAX = 5;

class Stack {

public:
    Stack();
    int pop();
    int getStackTop();
    void push(int v);
    bool isEmpty();
    void printStack(void) const;

private:
    unsigned n;
    int stack[MAX];

};

#endif // STACK_H

Stack.cpp Stack.cpp

#include <iostream>
#include "Stack.h"

using namespace std;

Stack::Stack() {
    this->n  = 0;
}

/* More code here ... */ / *更多代码在这里... * /

Error: 错误:

duplicate symbol _MAX in:
    /var/folders/r3/zbrrqh7n5tg0vcnpr9_7j0nr0000gn/T/p38-ac46b9.o
    /var/folders/r3/zbrrqh7n5tg0vcnpr9_7j0nr0000gn/T/Stack-a5d98e.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I've fixed CASE II just by removing the extern keyword, and case I was fixed using the #define MAX 5 in header instead of using some constant variable, the thing is even tough I've fixed the problem I want to have a better understanding of C++, a I would like to know the cause of those errors since I didn't get it quite well. 我仅通过删除extern关键字修复了CASE II,并在标题中使用#define MAX 5而不是使用某些常量来修复了问题,这甚至更棘手了,我已经修复了我想要更好的问题了解C ++,我想知道这些错误的原因,因为我不太了解它。 Can someone give me an explanation ? 有人可以给我一个解释吗? Thanks in advance! 提前致谢!

There is a difference between compile time constant and run time constant. 编译时间常数和运行时间常数之间存在差异。

extern const unsigned MAX;

declares a run time constant, not a compile time constant. 声明运行时常量,而不是编译时常量。 It can be initialized to 5, 10, 20, or anything else at run time. 可以在运行时将其初始化为5、10、20或其他任何值。 Once initialized, its value remain constant. 初始化后,其值将保持不变。

Since it is not a compile time constant, it cannot be used as the size of an array. 由于它不是编译时间常数,因此不能用作数组的大小。

To use it as compile time constant, use: 要将其用作编译时间常数,请使用:

const unsigned MAX = 5;

in the .h file. 在.h文件中。


extern const unsigned MAX = 5;

does not work since that not only declares the variable but defines it too. 不起作用,因为它不仅声明了变量,还定义了变量。 Any .c file that #include s the .h file ends up defining the variable, which explain the duplicate symbol linker error. 任何#include到.h文件的.c文件最终都定义了变量,该变量解释了重复的符号链接器错误。

int stack[MAX]; int stack [MAX];

your error lies here u have to compulsorily specify the size. 您的错误就在这里,您必须强制指定大小。 eg int stack[20]; 例如int stack [20];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM