【问题标题】:Stack auto variable in c++C ++中的堆栈自动变量
【发布时间】:2021-12-18 15:21:46
【问题描述】:

我是c++新手,想用自动变量构建一个堆栈,但是,在数组中得到一系列错误,dr可以帮我修复它,谢谢。显然数组 a 被错误地声明并且 top() 函数发生冲突,但我没有看到这个问题,因为我对自动变量不太熟悉,所以我有点迷茫。这是代码和错误:

#include <bits/stdc++.h>
#include <array>
#include <iostream>

using namespace std;

#define MAX 1000

class Stack {
    int top;

public:
    auto a[MAX];

    Stack() { top = -1; }
    bool push(auto x);
    auto pop();
    auto top();
    bool isEmpty();
};

bool Stack::push(auto x)
{
    if (top >= (MAX - 1)) {
        cout << "Stack Overflow";
        return false;
    }
    else {
        a[++top] = x;
        cout << x << " pushed into stack\n";
        return true;
    }
}

auto Stack::pop()
{
    if (top < 0) {
        cout << "Stack Underflow";
        return 0;
    }
    else {
        auto x = a[top--];
        return x;
    }
}
auto Stack::top()
{
    if (top < 0) {
        cout << "Stack is Empty";
        return 0;
    }
    else {
        auto x = a[top];
        return x;
    }
}

bool Stack::isEmpty()
{
    return (top < 0);
}

int main() {
    class Stack stk;
    stk.push(4);
    stk.push(5);
    stk.push(6);

}

错误:

In file included from asgt.h:6,
                 from asgt.cpp:1:
graph.h:45:10: error: ‘a’ declared as array of ‘auto’
   45 |     auto a[MAX];
      |          ^
graph.h:48:15: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   48 |     bool push(auto x);
      |               ^~~~
graph.h:50:14: error: ‘auto Stack::top()’ conflicts with a previous declaration
   50 |     auto top();
      |              ^
graph.h:42:9: note: previous declaration ‘int Stack::top’
   42 |     int top;
      |         ^~~
graph.h:54:18: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’
   54 | bool Stack::push(auto x)
      |                  ^~~~
graph.h: In member function ‘bool Stack::push(auto:2)’:
graph.h:61:9: error: ‘a’ was not declared in this scope
   61 |         a[++top] = x;
      |         ^
graph.h: In member function ‘auto Stack::pop()’:
graph.h:74:18: error: ‘a’ was not declared in this scope
   74 |         auto x = a[top--];
      |                  ^
graph.h: At global scope:
graph.h:78:6: error: no declaration matches ‘auto Stack::top()’
   78 | auto Stack::top()
      |      ^~~~~
graph.h:42:9: note: candidate is: ‘int Stack::top’
   42 |     int top;
      |         ^~~
graph.h:41:7: note: ‘class Stack’ defined here
   41 | class Stack {
      |       ^~~~~
asgt.cpp:17:26: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts’

【问题讨论】:

  • 类成员不能用auto 声明。您是否尝试拥有任何类型的数组?为此,您将需要模板。
  • 编译器如何推断1000个元素的数组“a”中的数据类型是什么?
  • 您似乎正在尝试使用auto,就好像它是any。

标签: c++ stack declaration auto typespec


【解决方案1】:

所用编译器的错误信息含义如下。

这样的数组声明

auto a[MAX];

无效。编译器无法确定数组的实际类型。禁止在数组声明中使用说明符 auto。

函数参数不能使用说明符 auto 声明。

bool push(auto x);

数据成员和成员函数不能同名

int top;
//...
auto top();

【讨论】:

  • "函数参数不能用说明符auto声明" 在C++20中是合法的。我们生活在未来,不是吗? :P
  • @HolyBlackCat 他正在使用编译器,根据编译器错误不允许这样的功能。
【解决方案2】:

您需要为此使用templates。 Stack 类的声明应该类似于:

template <class T>
class Stack<T> {
    int top;

public:
    T a[MAX];

    Stack() { top = -1; }
    bool push(const T& x); // can't afford to copy if your Stack is general purpose
    T pop();
    T get_top(); // can't have same name for member and method
    bool isEmpty();
};

另外,您可能希望重载 push() 以便同时通过 const 引用和左值引用传递(即,移动堆栈中的某些内容)。而且您肯定想将a 设为私有。

【讨论】:

    猜你喜欢
    • 2017-12-02
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多