2

我已经构建了一个静态的结构堆栈,并且一切正常 - 包括创建一个结构数组。但是,由于某种原因,我无法将数组的顶部设置为结构。

在我的.cpp文件中,在我的push函数中,我一直在以下行出错:

stackArray[top] = newStudent;

我收到的错误是:

"51: 'operator=' 不匹配' (((studentstack )this)->studentstack::stackArray + (+(((unsigned int)((studentstack*)this)->studentstack::top) * 24u ))) = ((studentstack*)this)->studentstack::newStudent'"

我在下面包括我的代码。

头文件:

#ifndef studentstack_H
#define studentstack_H

#include <iostream>
#include <string>

using namespace std;

class studentstack {
    private:
        int size;         // Stack size
        int top;          // Top of the Stack

        struct student {
            int ID;
            string Name;
            string Address;
            double GPA; 
        };
        student * stackArray;  // Pointer to the stack
        student * newStudent; // Pointer to the new student

    public: //Constructor
        studentstack(int);

        // Copy Constructor
        studentstack(const studentstack &);

        //Destructor
        ~studentstack();

        //Stack Operaations
        void push(string, int, double, string);
        void pop(int &);
        bool isFull() const;
        bool isEmpty() const;
    };

#endif

学生栈.cpp

#include <iostream>
#include "studentstack.h"

using namespace std;

studentstack::studentstack(int SIZE) {
    stackArray = new student[SIZE];
    size = SIZE;
    top = -1;
    int ID = 0;
    double GPA = 0;
}

studentstack::studentstack(const studentstack &obj) {
    if (obj.size > 0)
        stackArray = new student[obj.size];
    else
        stackArray = NULL;

    size = obj.size;

    for (int count = 0; count < size; count++)
        stackArray[count] = obj.stackArray[count];

    top = obj.top;
}

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

void studentstack::push(string name, int id, double gpa, string address) {
    if (isFull()) {
        cout << "The stack is full.\n";
    } else {
        top++;
        newStudent = new student;
        newStudent-> Name = name;
        newStudent-> ID = id;
        newStudent-> Address = address;
        newStudent-> GPA = gpa;
        stackArray[top] = newStudent;
    }
}   

void studentstack::pop(int &id) {
    if (isEmpty()) {
        cout << "The stack is empty.\n";
    } else {
        id = stackArray[top].ID;
        top--;
    }
}

bool studentstack::isFull() const {
    bool status;

    if (top == size - 1)
        status = true;
    else
        status = false;

    return status;
}

bool studentstack::isEmpty() const {
    bool status;

    if (top == -1)
        status = true;
    else
        status = false;

    return status;
}

主文件

#include "studentstack.h"
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int id, var;
    string address;
    double gpa;
    studentstack s[20];

    for(int x =0; x<20; x++) {
        cout << "New Student Name: " << endl;
        cin >> name;

        cout << "ID: " << endl;
        cin >> id;

        cout << "GPA: " << endl;
        cin >> gpa;

        cout << "Address: " << endl;
        cin >> address;

        s.push(name, id, gpa, address)
    }

    cout << "Popping: "
    for(int x = 0; x < 5; x++) {
        s.pop(var);
        cout <<var;
    }

    return(0);
}
4

2 回答 2

4

您可能希望将示例缩减为显示问题的最小代码段。归结为您尝试将 a 分配给对象student*数组中的元素。student指向对象的指针与对象不同:

stackArray[0] = new student(); // does NOT work
stackArray[0] = student();

请注意,该对象是由构造函数在 C++ 中创建的,不一定涉及new. 当您使用构造和对象时,new您仍然会创建一个对象,但您也会在堆上为其分配空间。默认情况下,对象在任何定义的地方创建。例如,student()在堆栈上创建一个对象,然后将其分配给stackArray[0].

于 2012-12-08T00:08:20.800 回答
0

与您的问题没有直接关系,但请注意您的 main() 无法工作。studentstack您声明一个包含 20 个元素的数组:

studentstack s[20];

后来你正在做:

s.push(/* ... */);
s.pop(/* ... */);

这没有多大意义。s不是一个studentstack。它是一个s数组studentstackC++ 中的数组没有成员函数。

于 2012-12-08T00:11:17.747 回答