我正在做一個任務,我被要求在C++中實現鏈表。到目前爲止,除了當我創建一個新列表時,一切都很好。在我的方法create_list()
。在將內容和身份號碼分配給我的Field
並嘗試撥打GetNext()
後,我收到一條錯誤消息:Request for member 'GetNext()' in 'Node' which is a non-class type '*Field'.
我對C++語法和麪向對象編程還不熟悉。我究竟做錯了什麼?我想通過使用行Field *Node = new Field(SIZE, EMPTY);
我的變量Node
將是類型Field
...?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()方法:
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;
}
}
真棒謝謝....現在,我真的覺得它更有意義.... – accraze