Je travaille sur une affectation où l'on me demande d'implémenter une liste chaînée en C++. Jusqu'à présent, tout fonctionne bien, sauf quand je crée une nouvelle liste. dans ma méthode create_list()
. après avoir attribuer le contenu et un numéro d'identification à mon Field
et essayer d'appeler GetNext()
j'obtiens une erreur en disant: Request for member 'GetNext()' in 'Node' which is a non-class type '*Field'.
Je suis encore nouveau à la syntaxe C++ et la programmation orientée objet. Qu'est-ce que je fais mal? Je pensais en utilisant la ligne Field *Node = new Field(SIZE, EMPTY);
que ma variable Node
serait du type de classe Field
...?Implémentation de la liste chaînée 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;
ici est ma méthode 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;
}
}
génial merci .... qui fait plus de sens maintenant que je pense à ce sujet .... – accraze