Вызов метода на struct в связанном списке
ошибка возникает, когда мы вызываем ship_location
p_enemy. ship_location (); / / он должен отображать местоположение корабля
error. ship_location должен иметь struct/union/class слева
выражение ошибки должно иметь тип класса
Что я уже пробовал:
// 119 method declaration and call syntax.cpp #include "stdafx.h" #include <iostream> #include <ctime> #include <cstdlib> using namespace std; // will return integer between high and low value unsigned int rand_range(int high, int low) { return rand() % (high - low) + low; } //defination of a ship struct enemy_space_ship { int x_coordinates; int y_coordinates; int power; enemy_space_ship * p_next_ship; void ship_location() { std::cout << "x coordinates :" << x_coordinates << std::endl; std::cout << "y coordinates :" << y_coordinates << std::endl; } }; // Will create new ship on being call and link last ship to p_next_ship pointer enemy_space_ship * get_new_ship(enemy_space_ship * p_enemy) { enemy_space_ship * p_new_ship = new enemy_space_ship; p_new_ship->x_coordinates = rand_range(1000, 100); p_new_ship->y_coordinates = rand_range(1000, 100); p_new_ship->power = rand_range(10, 1); p_new_ship->p_next_ship = p_enemy; return p_new_ship; } void searching_ship(enemy_space_ship * p_enemy, int power_limit) { while (p_enemy != NULL) { if (p_enemy->power > power_limit) { // call method inside the struct to display x and y coordinates of ship p_enemy.ship_location(); //error is here } p_enemy = p_enemy->p_next_ship; } std::cout << "Seaching done !!" << std::endl; } int main() { srand(time(NULL)); int user_selection = 1; enemy_space_ship * p_enemy = NULL; // Each call create a new ship while (user_selection != 0) { p_enemy = get_new_ship(p_enemy); std::cout << "To create another ship enter any integer or ( 0 to exit ) "; std::cin >> user_selection; } // user will enter power and program will show all ships that are above defined power std::cout << "Enter the ship power limit :"; std::cin >> user_selection; searching_ship(p_enemy, user_selection); std::cin.ignore(); }