无法与'operator='匹敌

no match for 'operator='

本文关键字:匹敌 operator      更新时间:2023-10-16

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

在我的.cpp文件中,在我的push函数中,我在以下行上不断出错:

stackArray[top] = newStudent;

我收到的错误是:

"51: 与'(((studentstack)this)->studentstack::stackArray + (+(((unsigned int)((studentstack*)this)->studentstack::top) *

24u))) = ((studentstack*)this)->studentstack::newStudent' 中的'operator=' 不匹配

我在下面包含我的代码。

头文件:

#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;
}

主.cpp

#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);
}

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

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

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

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

studentstack s[20];

稍后你正在做:

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

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