我的菜单中未处理的异常

Unhandled exception within my menu

我目前在我的代码中遇到一个未处理的异常,我不知道为什么。这是我第一次同时使用多个 class。

目前我正在尝试将用户输入从另一个 class 放入字符串中。我正在尝试将用户输入输入

下面的 class 中名为 name 的字符串
#ifndef SHIP_H
#define SHIP_H
#include "ApplicationMenu.h"
#include <string>
class Ship
{
public:
    Ship(void);
    ~Ship(void);

    std::string _size;
    std::string _shipName;
    std::string name;
};

#endif

进入main运行下面的函数

#include "ApplicationMenu.h"
#include "Ship.h"
#include <string>
#include <sstream>

class Ship;

#include <iostream>

using namespace std;

ApplicationMenu::ApplicationMenu(void) { userChoice = 0; }


ApplicationMenu::~ApplicationMenu(void) { }



void ApplicationMenu::displayMenu() {


    cout << "Welcome to the Port." << endl << "Please select one of the
        following options : " << endl
        << "1: Dock Ship" << endl;
    cin >> userChoice;
    switch (userChoice)
    {
    case 1:

        Ship*   ship;

        ship->name;


        cout << "Please enter the name of your ship your wish to dock: ";
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        getline(cin, ship->name);

        cout << ship->name;
        break;
    }
}

谁能告诉我为什么会出错?

编辑:

此错误是由于未初始化的指针引起的,这是由于缺乏有关 C++ 指针的知识。尽管社区对此做出了回答,但此 link 对未来的观众很有帮助。 http://www.cplusplus.com/doc/tutorial/pointers/

您有一个未初始化的指针Ship* ship;。您需要使用 Ship* ship = new Ship(); 或将船舶声明为 Ship ship;

Ship* ship 是一个指向随机内存位置的单元化指针,您随后会尝试访问该位置。您应该始终将指针初始化为 nullptr 或有效对象以防止未定义的行为。

Ship* s( new Ship );