简体   繁体   中英

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:

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

#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

#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. 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. 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.


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.

int stack[MAX];

your error lies here u have to compulsorily specify the size. eg int stack[20];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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