【问题标题】:'arr' was not declared in this scope when declaring arr in default constructor在默认构造函数中声明 arr 时未在此范围内声明“arr”
【发布时间】:2020-11-25 05:38:47
【问题描述】:

我是 C++ 新手。我正在尝试实现一个 stack。我在 默认构造函数 中声明了一个 arr 命名变量。

但是当我编译我的代码时,我得到一个错误提示

'arr' was not declared in this scope

我的代码:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

class Stack
{

private:
    int top = -1;
    int n = 100;

public:
    Stack()
    {
        int arr[n]; // 100 element stack
    }

    void push(int element)//push element to the top of the stack
    {
        if (isFull() == false)
        {
            // push element
            top += 1; //increment top
            arr[top] = element;
        }
        else cout << "\nStack is full! Can't push element\n";
    }

    void pop()
    {
        if (isEmpty() == false)
        {
            top -= 1;//decrement top
        }
    }

    bool isEmpty()
    {
        if (top == -1)
            return true;
        else
            return false;
    }

    bool isFull()
    {
        if (top == n - 1)
            return true;
        else
            return false;
    }

    int peek(int position)// item at specific location
    {
        if (position > top)
        {
            cout << "\nInvalid position\n";
            return -1;
        }
        else
        {
            return arr[position];
        }
    }

    int count()// number of items
    {
        return top + 1;
    }

    void change(int position, int value) // change item at specific location
    {
        if (position > top)
        {
            cout << "\nInvalid postion\n";
        }
        else
        {
            arr[position] = value;
        }
    }

    void display() // display elements stored
    {
        if (isEmpty() == false)
        {
            cout << endl;
            for (int i = 0; i < top; i++)
            {
                cout << arr[i] << endl;
            }
        }
        else
        {
            cout << endl << "Stack is empty! No elements to display" << endl;
        }
    }
};

int main()
{
    Stack st;

    cout << endl;
    cout << st.isEmpty();

    st.push(10);

    cout << endl;
    cout << st.isEmpty();

    st.display();

    return 0;
}

我的错误:

stack.cpp: In member function 'void Stack::push(int)':
stack.cpp:28:4: error: 'arr' was not declared in this scope
   28 |    arr[top] = element;
      |    ^~~
stack.cpp: In member function 'int Stack::peek(int)':
stack.cpp:68:11: error: 'arr' was not declared in this scope
   68 |    return arr[position];
      |           ^~~
stack.cpp: In member function 'void Stack::change(int, int)':
stack.cpp:85:4: error: 'arr' was not declared in this scope
   85 |    arr[position] = value;
      |    ^~~
stack.cpp: In member function 'void Stack::display()':
stack.cpp:96:11: error: 'arr' was not declared in this scope
   96 |     cout<<arr[i]<<endl;
      |           ^~~

我不明白为什么会这样。

arr 不应该是所有成员函数都可以访问的吗?

【问题讨论】:

  • arrntop 没有区别。你在那里做的是正确的事。
  • @cigien 请澄清。 int arr[n]Stack() 中,这意味着它是 Stack() 的本地地址,不是吗?
  • int arr[n]; // 100 element stack 是一个仅存在于构造函数内部的局部变量。它也是一个 VLA,在标准 c++ 中是不合法的。
  • 是的。这就是为什么编译器说你不能在其他成员函数中使用它。
  • “不应该是所有成员函数都可以访问的 arr 吗?” - 为什么会这样?确保支持断言。

标签: c++ class c++11 constructor class-members


【解决方案1】:

int arr[n]; // 100 element stack 

是仅存在于构造函数(作用域)内的局部变量。其他成员不会知道这个数组,这就是错误的原因。

将数组声明移动到私有部分,n 应该在编译时知道

class Stack
{

private:
   int top = -1;
   static constexpr int n = 100;  // n as static constexpr
   int arr[n]{ 0 };               // move `arr` here and optionally initlize to `0`

public:
   Stack() = default;
   // ... rest of the code

};

附带说明,当您执行数组操作时(即像在push 成员中一样)对传递的position 进行绑定检查。

【讨论】:

  • int arr[n]{0}; 上的小注释,它不会初始化为 0,因为传入了 0,它初始化为 0,因为它默认是这样初始化的。写 int arr[n] { 2 }; 不会将元素初始化为 2,它只会将第一个初始化为 2,其余的初始化为 0。
  • @Tas 是的,你是对的。我想初始化数组ele。到0's。还有could have been simply int arr[n]{ };.
【解决方案2】:

这是因为 arr 不是类成员,而只是构造函数中定义的局部变量。它与构造函数作用域结束一起消失。

这样开始你的课程:

class Stack
{
  int top = -1;
  static int const n = 100;
  int arr[n]; // 100 element stack                                                                                                                    

public:
  Stack() {}
  // ...
}

【讨论】:

    猜你喜欢
    • 2018-05-29
    • 1970-01-01
    • 2016-01-23
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多