0

我为学生记录创建了一个数组,并且应该将它弹出到我的堆栈中。除了我的 stack.pops 和 MAIN 中的 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;
}
4

2 回答 2

6
Studentrecords stack();

不声明一个Studentrecords命名的stack,它声明一个stack返回一个命名的函数Studentrecords。将其更改为

Studentrecords stack;

此外,您的类至少需要一个复制构造函数和赋值运算符。

于 2012-09-20T23:08:09.560 回答
0

你能发布编译器的错误吗?还是产生的输出与预期的输出?没有那个,我不得不说你的 pop 函数不接受参数并且你正在传递它 catchVar ......这将是一个编译器错误。

于 2012-09-20T23:11:23.753 回答