2012-11-01 3 views
0

나는 C++에서 연결된 목록을 구현하도록 요청받는 작업을하고 있습니다. 지금까지는 새로운 목록을 만들 때를 제외하고 모든 것이 훌륭하게 작동합니다. 내 방법은 create_list()입니다. 내용과 ID 번호를 Field에 할당하고 GetNext()으로 전화를 걸면 오류 메시지가 나타납니다. Request for member 'GetNext()' in 'Node' which is a non-class type '*Field'. 저는 C++ 구문 및 객체 지향 프로그래밍에 아직 익숙하지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 내 변수 Node이 클래스 유형 Field ...이라는 라인 Field *Node = new Field(SIZE, EMPTY);을 사용하여 생각 했습니까?C++ 연결된 목록 구현

#include <iostream> 
#include <ctype.h> 

using namespace std; 

typedef enum { EMPTY, OCCUPIED } FIELDTYPE; 

// Gameboard Size 
int SIZE; 

class Field { 

private: 
int _SquareNum; 
FIELDTYPE _Content; 
Field* _Next; 

public: 
// Constructor 
Field() { } 

// Overload Constructor 
Field(int SquareNum, FIELDTYPE Entry) { _SquareNum = SquareNum; _Content = Entry; } 

// Get the next node in the linked list 
Field* GetNext() { return _Next; } 

// Set the next node in the linked list 
void SetNext(Field *Next) { _Next = Next; } 

// Get the content within the linked list 
FIELDTYPE GetContent() { return _Content; } 

// Set the content in the linked list 
void SetContent(FIELDTYPE Content) { _Content = Content; } 

// Get square/location 
int GetLocation() { return _SquareNum; } 

// Print the content 
void Print() { 

    switch (_Content) { 

     case OCCUPIED: 
      cout << "Field " << _SquareNum << ":\tOccupied\n"; 
      break; 
     default: 
      cout << "Field " << _SquareNum << ":\tEmpty\n"; 
      break; 
    } 

} 

}*Gameboard; 

여기 내 create_list() 방법 : 당신은 Node.GetNext()를 호출하고

void create_list() 
{ 
int Element; 


cout << "Enter the size of the board: "; 
cin >> SIZE; 
for(Element = SIZE; Element > 0; Element--){ 
    Field *Node = new Field(SIZE, EMPTY); 
    Node.GetNext() = Gameboard; // line where the error is 
    Gameboard = Node; 
    } 
} 

답변

1

없음을 사용하는 것입니다.

클래스에 대한 포인터가 있고 해당 클래스의 멤버에 액세스하려는 경우 ->을 사용하면 간단합니다.

Node->GetNext() = Gameboard; 

코드에 다른 오류가있는 것으로 생각됩니다.이 수정 프로그램을 사용하더라도 작동하지 않을 것이라고 생각합니다. 아마도 당신이 원하는 것은 아마도

Node->SetNext(Gameboard); 
+0

끝내 주셔서 감사합니다 .... 실제로 생각해 보면 실제로 더 의미가 있습니다 .... – accraze

1

하지만 Node는 포인터입니다. Node->GetNext()에서와 같이 . 연산자 대신 -> 연산자를 사용해야합니다.

+0

이를 시도한 좌변으로 기능을 사용할 수 있지만 지금이 오류가 무엇입니까 : "좌변 할당의 왼쪽 피연산자로 필요" – accraze

+0

을 당신의 코드는 약간의 변화가 필요합니다 @SunHypnotic 내 대답을 참조하십시오. – john

3

.은 개체의 개체 및 개체 참조를 처리하는 데 사용됩니다. 그러나 Node은 개체에 대한 포인터입니다. 따라서 .과 함께 사용하려면 먼저 참조로 변환해야합니다. 이는 (*Node).GetNext()을 의미합니다. 또는 속기를 사용할 수 있습니다 : Node->GetNext() -이 두 개는 정확히 같습니다.

사용하는 것이 니모닉은

Field *Node = new Field(SIZE, EMPTY); 

노드가 필드에 입력 포인터입니다 선언에 포인터 뾰족한 운영자 :

0

입니다. l 값으로 설정하려면 함수가 참조 값을 반환해야합니다.

// Get the next node in the linked list 
Field& GetNext() { return *_Next; } 

은 다음

Node->GetNext() = *Gameboard;