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

标签 c++ arrays initialization stack constants

我知道在声明一个数组时我必须用一个常量值指定它的大小,但在这种情况下我创建了一个 const 值,它也是一个 const 表达式,用一个文字值初始化,可以在编译时间,但我在这两种情况下一直有错误:

案例一:

堆栈.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

堆栈.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;
    }
}

错误:

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];
            ^

在第二种情况下事情变得更加奇怪......

案例二:

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

堆栈.cpp

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

using namespace std;

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

/* 更多代码在这里... */

错误:

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)

我已经通过删除 extern 关键字修复了 CASE II,并且我是使用 header 中的 #define MAX 5 而不是使用一些常量变量来修复的,事情甚至很难我已经解决了我想要的问题对 C++ 有了更好的理解,我想知道这些错误的原因,因为我不太了解。有人可以给我一个解释吗?提前致谢!

最佳答案

编译时常量和运行时常量是有区别的。

extern const unsigned MAX;

声明一个运行时常量,而不是编译时常量。它可以在运行时初始化为 5、10、20 或任何其他值。一旦初始化,它的值保持不变。

因为它不是编译时常量,所以不能用作数组的大小。

要将其用作编译时间常量,请使用:

const unsigned MAX = 5;

在 .h 文件中。


extern const unsigned MAX = 5;

不起作用,因为它不仅声明了变量,还定义了变量。 #include .h 文件的任何 .c 文件最终定义了变量,这解释了重复符号链接器错误。

关于C++ : Error Initilializing Array Size with Const Variable in multiple files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40701205/

相关文章:

c++ - operator() 的典型用途是什么

arrays - 在 Swift 中处理包含数组的结构体的指针

arrays - 处理中的 SplitTokens 问题

c++ - 如何在声明时初始化成员对象?

c++ - 通过默认字符串简化 vector 中字符串的初始化

c++ - 关于标准 :cout in C++

c++ - 在 Windows 7/8/10 Win32 C++ 中检测/识别显示器连接的端口(HDMI、其他)

c++ - 何时使用 std::string 与 char*?

arrays - Ocaml 中的数组操作

initialization - Vimrc 命令排序