簡體   English   中英

C ++為什么我的數組沒有彈出到堆棧中?

[英]C++ why does my array not pop into my stack?

我為studentrecords創建了一個數組,應該將其彈出到堆棧中。好吧,除了我在main中的stack.pops和stack.pushes之外,其他所有東西都正常工作...我已經接近完成該程序了,我想知道是否有人知道解決方案?

#include <iostream>
#include <list>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>

using namespace std;

class Studentrecords
{
private:
    struct student
    {
        string name;
        string address;
        int ID;
        double gpa;
    };

    student *stackArray;
    int stackSize;
    int top;

public:
    Studentrecords();
    Studentrecords(int size);
    ~Studentrecords();
    void push(string name, string address, int id, double gpa);
    void pop();
    bool isFull() const;
    bool isEmpty() const;
    void display();
};

Studentrecords::Studentrecords(int size)
{
    stackArray = new student[size];
    top = -1;
}

Studentrecords::Studentrecords()
{
    stackSize = 20;
    stackArray = new student[stackSize];
    top = -1;
}

Studentrecords::~Studentrecords()
{
    delete [] stackArray;
}

void Studentrecords::push (string name, string address, int id, double gpa)
{
    if (isFull())
    {
        cout << "The stack is full!" << endl;
    }
    else
    {
        student newStudent;
        newStudent.name = name;
        newStudent.address= address;
        newStudent.ID = id;
        newStudent.gpa = gpa;
        stackArray[top] = newStudent;
        top++;
    }
}

void Studentrecords::pop ()
{
    if (isEmpty())
    {
        cout << "The stack is empty!" << endl;
    }
    else
    {
        cout << stackArray[top-1].name << endl;
        cout << stackArray[top-1].address << endl;
        cout << stackArray[top-1].ID << endl;
        cout << stackArray[top-1].gpa << endl;
        top--;
    }
}

bool Studentrecords::isFull() const
{
    bool status;
    if (top == stackSize - 1)
        status = true;
    else
        status = false;
    return status;
}

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

void Studentrecords::display()
{
    for (int i = 0; i< top; i++)
    {
        cout << stackArray[i].name << endl;
        cout << stackArray[i].address << endl;
        cout << stackArray[i].ID << endl;
        cout << stackArray[i].gpa << endl << endl;
    }
}

int main()
{
    int catchVar;

    Studentrecords stack();

    cout << "Pushing 1st";
    stack.push("Jonny", "123 ave", 2343, 3.2);

    cout << "pushing 2nd";
    stack.push("Robby", "123 ave", 2343, 3.2);

    cout << "Popping ";
    stack.pop(catchVar);
    cout << catchVar << endl;

    cout << "Popping ";
    stack.pop(catchVar);
    cout << catchVar << endl;

    return 0;
}
Studentrecords stack();

沒有聲明一個名為Studentrecords stack ,它聲明了一個名為stack的函數,該函數返回一個Studentrecords 更改為

Studentrecords stack;

另外,您的類至少需要一個復制構造函數和賦值運算符。

您可以發布編譯器的錯誤嗎? 還是生產的產出與預期的產出? 否則,我不得不說您的pop函數不接受參數,而是將其傳遞給catchVar ...這將是編譯器錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM