C++ 出现错误“数组绑定不是']'标记之前的整数常量”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16449359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:22:08  来源:igfitidea点击:

Getting error "array bound is not an integer constant before ']' token"

c++arrays

提问by user1849859

I am trying to implement a stack using an array but I receive an error.

我正在尝试使用数组实现堆栈,但收到错误消息。

class Stack{
private:
    int cap;
    int elements[this->cap]; // <--- Errors here
    int top;
public:
  Stack(){
     this->cap=5;
     this->top=-1;
};

The indicated line has these errors:

指示的行有以下错误:

Multiple markers at this line
- invalid use of 'this' at top level
- array bound is not an integer constant before ']' token

What am I doing wrong?

我究竟做错了什么?

回答by templatetypedef

In C++, the size of an array must be a constant known at compile-time. You'll get an error if that isn't the case.

在 C++ 中,数组的大小必须是编译时已知的常量。如果不是这种情况,您将收到错误消息。

Here, you have

在这里,你有

int elements[this->cap];

Notice that this->capisn't a constant known at compile-time, since it depends on how big capis.

请注意,这this->cap不是编译时已知的常量,因为它取决于有多大cap

If you want to have a variably-sized array whose size is determined later on, consider using std::vector, which can be resized at runtime.

如果您想要一个大小可变的数组,其大小稍后确定,请考虑使用std::vector,它可以在运行时调整大小。

Hope this helps!

希望这可以帮助!

回答by Mohamad Ali Baydoun

You cannot use thisin the declaration like that. thisis a constant pointer passed to non-static methods in your class. It does not exist outside of that scope.

你不能this在这样的声明中使用。 this是传递给类中非静态方法的常量指针。它不存在于该范围之外。

Such array declarations need constant values/expressions for the size. You don't want that, you want a dynamicly sized container. The solution is to use a std::vector.

这样的数组声明需要大小的常量值/表达式。你不想要那个,你想要一个动态大小的容器。解决方案是使用std::vector.

回答by andre

Since other have already explained the cause of this issue, here is a possible solution to resolve it. Since it seems you may not know the array size at compile time and the assignment may restrict the use of std::vector<int>consider using a pointer implementation.

由于其他人已经解释了这个问题的原因,这里有一个可能的解决方案来解决它。由于您似乎在编译时可能不知道数组大小,并且赋值可能会限制std::vector<int>考虑使用指针实现的使用。

#include <algorithm>

class Stack{
private:
    int cap;
    int* elements; // use a pointer
    int top;
public:
    Stack(){
        this->cap=5;
        this->top=-1;
        elements = new int[this->cap];
    }

    Stack(const Stack& s) 
         : cap(s.cap) , top(s.top), elements(NULL)
    {
         if(cap > 0) {
             elements = new int[cap];
         }

         std::copy(s.elements , s.elements + cap, elements );
    }

    Stack& operator=(Stack s) {
         swap(s, *this);
         return  *this;
    }

    ~Stack() {delete [] elements;}

    friend void swap(Stack& first, Stack& second)
    {
        using std::swap; 
        swap(first.top, second.top);
        swap(first.cap, second.cap);
        swap(first.elements, second.elements);
    }
};

回答by vitalis emanuel setiawan

Change

改变

int elements[this->cap];

to

int* elements=new int[cap]