Я создаю игру на C++, в которой мне нужно создавать сетку ячеек и управлять ею с помощью указателей. Сетка представлена связанной структурой, где каждая ячейка имеет указатели на соседей (в том числе вверх, вниз, влево, вправо и т. д.). Однако при попытке распечатать сетку отображается только верхняя строка, а строка->neighbours[4] (которая должна перейти к следующей строке) становится nullptr после первой итерации.
Я считаю, что связал указатели правильно, но не знаю, почему это происходит. Вот мой код, и я надеюсь, что кто-нибудь поможет определить, что может быть не так.
Код: Выделить всё
// file coffee_board.cc
#include
#include "coffee_board.h"
using namespace std;
//--------------------------------------------------------------------
// Inputs from user when typing in the desired option
// Reads a character input while ignoring newlines
char get_input() {
char input; // Input from user
input = cin.get(); // Read input from user
while (input == '\n') { // Keeps going until first not enter
input = cin.get(); // Reads first input not enter
}
return input; // returns first not enter input
}
// Reads a number input while ignoring newlines
int get_number() {
char input;
input = get_input(); // Get input from user (not new line)
int number = 0; // Number from user input initially at 0
int out_number = 0; // Number output
while (input != '\n') { // Keep reading until enter
if ('0' neighbours[0] = temp_above;
temp_below->neighbours[1] = temp_above->neighbours[2];
temp_below = temp_below->neighbours[2];
temp_above = temp_above->neighbours[2];
}
}
void coffee_board::build() {
grid* prev_row = nullptr;
grid* next_row = nullptr;
make_row(next_row); // Create the first row
prev_row = next_row; // Set the first row as previous
for (int i = 1; i < height; i++) {
make_row(next_row); // Create the next row
link(prev_row, next_row); // Link the previous row to the current row
prev_row = next_row; // Move to the next row
}
input = prev_row; // Set the last row as the board's starting point
}
void coffee_board::print() {
if (input == nullptr) {
cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79236140/issue-with-navigating-linked-grid-nodes-in-c-row-neighbours4-becomes-nullp[/url]
Мобильная версия