Joshua McMullen
Joshua McMullen

Reputation: 33

no match for 'operator='

I have built a static stack of structures, and everything works - including creating an array of the structures. However, for some reason I can't set the top of the array to a structure.

In my .cpp file, in my push function, I keep erroring on the following line:

stackArray[top] = newStudent;

The error I am receiving is:

"51: no match for 'operator=' in '(((studentstack)this)->studentstack::stackArray + (+(((unsigned int)((studentstack*)this)->studentstack::top) * 24u))) = ((studentstack*)this)->studentstack::newStudent' "

I am including my code below.

Header file:

#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

studentstack.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;
}

main.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);
}

Upvotes: 2

Views: 1457

Answers (2)

Nikos C.
Nikos C.

Reputation: 51910

Not directly related to your question, but note that your main() cannot work. You declare an array of 20 studentstack elements:

studentstack s[20];

and later on you're doing:

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

That doesn't make much sense. s is not a studentstack. It's an array of studentstacks. Arrays in C++ don't have member functions.

Upvotes: 0

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 154015

You might want to cut down the example to a minimal piece of code showing the problem. What it comes down to is that you try to assign a student* to a an element in an array of student objects. A pointer to an object is different to an object:

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

Note, that object are created in C++ by a constructor and not necessarily involving new. When you construct and object using new you still create an object but you also allocate space for it on the heap. By default, objects are created wherever they are defined. For example, student() creates an object on the stack which is then assigned to an object in the memory of stackArray[0].

Upvotes: 4

Related Questions