访问向量内部、另一个对象内部、超出范围的对象成员

时间:2021-05-23 20:14:23

标签: c++ class object vector stl

我是 C++ 新手,我正在为输出控制台制作一个菜单系统,只需使用 cin 和 cout。我做了一个这样的结构:

UI 对象有一个 MENU 对象向量,而每个 MENU 对象又都有一个 Item 对象向量。每个 Item 本质上都是一个菜单项,可以选择。然后我想使用我的 UI 对象来调用菜单页面 (showMenu()),然后打印该菜单的项目。

这是我的问题:当我调用我的 UI 函数时,它会打印一个菜单,然后项目表现得很奇怪。我的程序崩溃了,并抱怨我的向量超出范围? 当我使用 Menu print(draw()) 函数时,它自己运行,然后它可以工作,但是当我通过我的 UI 类执行它时,它崩溃了。

这是我的代码的简化版本,有析构函数、setter 和 getter,我只是在这里省略了它们:

class ui
{
public:
    ui();
    void addMenu(menu&);
    void showMenu(size_t menuNumber);
private:
    vector<menu> menus_;
    vector<menu>::iterator menuIt;
};
class menu
{
public:
    menu();
    menu(string name);
    string getMenuName();
    void makeItem(string name, size_t link, size_t up, size_t down, size_t right, size_t left, COORD position, int type);
    void draw();
private:
    vector<item> items_;
    vector<item>::iterator itemIt;
    string name_;
};
class item
{
public:
    item();
    item(string name, size_t link, size_t up, size_t down, size_t right, size_t left, COORD position, int type);
private:
    size_t      up_;
    size_t      down_;
    size_t      right_;
    size_t      left_;
    COORD       position_;
    size_t      link_;
    int         type_;
    string      name_;
};
void ui::showMenu(size_t menuNumber)
{
    this->menus_.at(menuNumber).draw();
}
void ui::addMenu(menu& menu)
{
    menus_.push_back(menu);
}
menu::menu(string name)
{
    name_ = name;
}
void menu::makeItem(std::string name, size_t link, size_t up, size_t down, size_t right, size_t left, COORD position, int type)
{
    item* newItem = new item(name, link, up, down, right, left, position, type);
    items_.push_back(*newItem);
}
void menu::draw()
{
    cout << name_ << endl; // THIS WORKS!

    // Print menu items: <- THIS DOES NOT WORK!
    for (itemIt = items_.begin(); itemIt!=items_.end(); itemIt++)
    {
        // Do Stuff to menu items!
    }
}
item::item(std::string name, size_t link, size_t up, size_t down, size_t right, size_t left, COORD position, int type)
{
    name_ = name;
    up_ = up;
    down_ = down;
    right_ = right;
    left_ = left;
    position_ = position;
    link_ = link;
    type_ = type;
}

在我的主要内容中,我制作了一个 UI 对象,然后继续使用 来制作菜单和项目。然后我调用以下内容:

Main:
#include "ui.h"
#include "item.h"
#include "menu.h"

#include <stdlib.h>
#include <iostream>
#include <Windows.h>
#include <vector>
#include <iterator>
#include <string>

int main()
{
    ui ui;

    // Menus:
    menu menu1("Main menu");
    ui.addMenu(menu1);
                                             // L, u, d, r, l, pos,      type
    menu1.makeItem("Add device",                2, 0, 1, 0, 0, { 4,11 }, 0);
    menu1.makeItem("All devices",               3, 0, 2, 0, 0, { 4,12 }, 0);
    menu1.makeItem("Activity",                  4, 1, 3, 0, 0, { 4,13 }, 0);
    menu1.makeItem("Exit",                      5, 2, 3, 0, 0, { 4,14 }, 0);

    menu1.draw(); // Which works!
    ui.showMenu(0); // Which crashes!

    return 0;
}

0 个答案:

没有答案
相关问题