2012-12-08 116 views
2

我已經構建了一個靜態的結構堆棧,並且一切正常 - 包括創建一個結構數組。但是,出於某種原因,我無法將數組的頂部設置爲結構。'operator ='不匹配

在我.cpp文件中,在我的push功能,我把示數以下行:

stackArray[top] = newStudent; 

我收到的錯誤是:

「51:敵不過「運營商='in'(((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 

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); 
} 
+0

它看起來像你試圖分配一個指針指向非指針。嘗試將你的stackArray聲明爲**,相應地修改你的新事物,看看會發生什麼。 – RonaldBarzell

回答

4

你可能想的例子削減到最小的代碼顯示問題。最重要的是,您嘗試將student*分配給student對象數組中的元素。一個指針,一個目的是爲一個對象不同:

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

筆記,該對象用C++創建由一個構造函數和不一定涉及new。當你使用new構造和對象時,你仍然創建一個對象,但是你也在堆上爲它分配空間。默認情況下,無論在何處定義對象,都會創建對象。例如,student()在堆棧中創建一個對象,然後將其分配給stackArray[0]內存中的對象。

0

與您的問題沒有直接關係,但請注意您的main()無法工作。你聲明的20個studentstack元素的數組:

studentstack s[20]; 

,後來你在做:

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

這並沒有太大的意義。 s不是studentstack。這是陣列studentstack s。 C++中的數組沒有成員函數。