我正在做一个任务,我被要求在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