I'm new to c ++ and I'm wanting to build a stack with automatic variables, however, get a series of errors in the array, dr can help me fix it thanks. Apparently the array a is wrongly declared and the top() function is in conflict, but I don't see this problem, as I have little familiarity with automatic variables I'm a little lost. Here's the code and errors:
#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);
}
Error:
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’
The error messages of the used compiler mean the following.
Such a declaration of an array
is invalid. The compiler is unable to determine the actual type of the array. Using the specifier auto with array declarations is prohibited.
A function parameter may not be declared with the specifier auto.
Data member and member function may not have the same name