Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 面向对象编程-C++;运动 #包括 #包括 #包括 #定义最大尺寸100 int pos=-1; 使用名称空间std; 类堆栈元素{ 私人: int数据; 字符串名; 公众: StackElement(){ 数据=0; name=“brak”; } StackElement(int d,string n){ 数据=d; name=n; } void getData(){ cin>>数据; } void getName(){ cin>>名称; } }; 类堆栈{ 私人: 整数堆栈大小; StackElement[]; 公众: 堆栈(){ stackSize=0; cout_C++_Oop_Stack - Fatal编程技术网

C++ 面向对象编程-C++;运动 #包括 #包括 #包括 #定义最大尺寸100 int pos=-1; 使用名称空间std; 类堆栈元素{ 私人: int数据; 字符串名; 公众: StackElement(){ 数据=0; name=“brak”; } StackElement(int d,string n){ 数据=d; name=n; } void getData(){ cin>>数据; } void getName(){ cin>>名称; } }; 类堆栈{ 私人: 整数堆栈大小; StackElement[]; 公众: 堆栈(){ stackSize=0; cout

C++ 面向对象编程-C++;运动 #包括 #包括 #包括 #定义最大尺寸100 int pos=-1; 使用名称空间std; 类堆栈元素{ 私人: int数据; 字符串名; 公众: StackElement(){ 数据=0; name=“brak”; } StackElement(int d,string n){ 数据=d; name=n; } void getData(){ cin>>数据; } void getName(){ cin>>名称; } }; 类堆栈{ 私人: 整数堆栈大小; StackElement[]; 公众: 堆栈(){ stackSize=0; cout,c++,oop,stack,C++,Oop,Stack,推送功能以 #include <iostream> #include <string> #include <cstdlib> #define MAX_SIZE 100 int pos=-1; using namespace std; class StackElement { private: int data; string name; public: StackElement() { data=0

推送功能以

#include <iostream> 
#include <string> 
#include <cstdlib> 
#define MAX_SIZE 100
int pos=-1;
using namespace std; 

class StackElement { 
private: 
    int data; 
    string name; 
public: 
    StackElement() { 
        data=0;
        name="brak";
        } 
    StackElement(int d, string n) { 
        data = d;
        name = n;
        } 
    void getData() { 
        cin>>data;
        } 
    void getName() {
        cin>>name;
        } 
};
class Stack {
private: 
    int stackSize; 
    StackElement element[]; 
    public:
    Stack() {
        stackSize = 0;
        cout<<"Stack is empty now."<<endl;
        }
    Stack(int s) {
        stackSize = s;
        }
    void push() {
        element[pos+1].getData();
        cout<<"Enter element name:"<<endl;
        element[pos+1].getName();
        pos++;
        }
    void pop() {
        pos--;
        }
    bool isEmpty() {
        if(stackSize==0)
             return true;
        }
    bool isFull() {
        if(pos==MAX_SIZE)
             return true;
        }
 }; 
int main() {
    Stack stack(MAX_SIZE);
    int choice;
    cout<<"1. Push in stack.\n2. Pop from stack.\n3. Display stack.\n4. Exit."<<endl;
    if(stack.isEmpty())
        cout<<endl<<"Stack is empty."<<endl<<endl;
    do { 
        cout<<"Enter your choice"<<endl;
        cin>>choice;
        switch(choice) {
            case 1:
                 cout<<"Enter value:"<<endl;
                 stack.push();
                 break;
            case 2:
                 stack.pop();
                 break;
            case 3:
                cout<<"Soon-to-be displayer"<<endl;
                 break;
            case 4:
                cout<<"Exit"<<endl;
                break;
            default:
                cout<<"Invalid choice"<<endl;
        }
        if(stack.isFull()) {
            cout<<"Stack is full!"<<endl;
            break;
        }
    } while (choice != 4);
return 0; 
}
pos
是一个全局变量,从-1开始,因此从0开始。 在尝试从不存在的元素获取数据之前,需要查看堆栈的大小

StackElement[];
需要调整大小或按如下方式声明:

element[pos+1].getData();

好的,我想出来了。如果有人需要类似的东西,下面是代码:

StackElement element[MAX_SIZE]; 
#包括
#包括
#包括
使用名称空间std;
类堆栈元素{
私人:
int数据;
字符串名;
公众:
StackElement(){
数据=0;
name=“-”;
}
StackElement(整数d,字符串n){
数据=d;
name=n;
}
void getElement(){
cin>>数据;

请调试您的代码并注意代码崩溃的确切位置。同时添加造成问题的输入。C样式数组具有静态大小。它不会自行调整大小。使用
std::vector
可调整大小的数组或预先声明数组的大小(并在达到最大大小时通知用户)。在编译器中打开警告。如果使用gcc/clang,请使用
-pedantic errors
StackElement[];
不应编译,因为没有提供大小。您想使用stl容器实现堆栈还是自己实现内存管理?您可以使用动态数组或动态列表。我按照您的建议声明了它,但现在我不能向堆栈中添加超过1个元素。这听起来像是一个新问题。问另一个问题stion,显示您是如何调用它的,以及出现了什么问题?将
pos
和堆栈大小放在本地似乎是明智的。您知道为什么以前不能将多个元素向后推吗?
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

class StackElement {
private:
    int data;
    string name;
public:
    StackElement() {
        data=0;
        name="-";
        }
    StackElement(int d, string n) {
        data = d;
        name = n;
        }
    void getElement() {
        cin>>data;
        cout<<"Enter name: "<<endl;
        cin>>name;
        }
};
class Stack {
private:
    int stackSize;
    int pos;
    StackElement *element;
public:
    Stack() {
        stackSize = 10;
        element = new StackElement[stackSize];
        }
    Stack(int s) {
        stackSize = s;
        element = new StackElement[stackSize];
        }
    void push() {
        element[pos++].getElement();
        ++stackSize;
        }
    StackElement pop() {
        return element[--pos];
        }
    bool isEmpty() {
        return pos == 0;
        }
    bool isFull() {
        return stackSize == pos;
        }
    void position() {
        pos=0;
    }
};

int main() {
    Stack stack(10);
    stack.position();
    int choice;
    cout<<"1. Push in stack.\n2. Pop from stack.\n3. Exit."<<endl;
    if(stack.isEmpty())
        cout<<endl<<"Stack is empty."<<endl<<endl;
    do {
        cout<<"Enter your choice"<<endl;
        cin>>choice;
        switch(choice) {
            case 1:
                cout<<"Enter value:"<<endl;
                stack.push();
                break;
            case 2:
                if(!stack.isEmpty())
                    stack.pop();
                else
                    cout<<"Stack is empty already."<<endl;
                break;
            case 3:
                cout<<"Exit"<<endl;
                break;
            default:
                cout<<"Invalid choice"<<endl;
        }
        if(stack.isFull()) {
            cout<<"Stack is full!"<<endl;
            break;
        }
    } while (choice != 4);
return 0;
}