2012-12-08 3 views
2

구조체의 정적 스택을 구축했으며 구조체 배열 생성을 포함하여 모든 것이 작동합니다. 그러나 어떤 이유로 배열의 맨 위를 구조로 설정할 수 없습니다.'operator ='에 대한 일치 항목이 없습니다.

stackArray[top] = newStudent; 

내가 수신하고 오류는 다음과 같습니다 :

"51 : 내 .cpp 파일에서

, 내 push 기능에, 나는 다음 줄에 erroring 없습니다 계속 '연산자에 대한 일치 = '('(studentstack :: top) * 24u))) = (((unsigned int)) '='in '(((studentstack) -> studentstack :: stackArray + 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()이 작동하지 않는다는 점에 유의하십시오.

studentstack s[20]; 

나중에 당신이하고있는에 :

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

훨씬 이해가되지 않습니다 당신은 20 개 studentstack 요소의 배열을 선언합니다. sstudentstack이 아닙니다. 배열studentstack입니다. C++의 배열에는 멤버 함수가 없습니다.

관련 문제