SIGSEGV我的程序出现故障

SIGSEGV Fault with my program

本文关键字:故障 程序 我的 SIGSEGV      更新时间:2023-10-16

IDE
代码块
明32制造3.82.90

下面是我的程序的调用堆栈

调用堆栈
0 0x45cb6b std::string::assign(std::string const&)()(??:?)
1 0x1??()(??:?)
2 0xf72ac0??()(??:?)
30x402332商店::initItem(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:107)
4 0x4020e3商店::initMenu(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:68)
50x402093商店::initShop(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:54)
60x401f58商店:商店(this=0xf72778)(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\shop.cpp:46)
70x4013c1 main()(C:\Users\Hethan\Desktop\Codeblock_Source\Coffee\main.cpp:10)

问题发生在#3。以下是相关部分的代码片段

1.导致SIGSEGV故障的方法

bool Shop::initItem()
{
    int count = 0, tempPrice;
    std::string input;
    std::stringstream tempSTRM;
    std::vector <Item>::iterator itemITR = menu.items.begin();
    Item tempITM;
    do
    {
        std::cout << "Input item name for item[" << count+1 << "]t:t";
        std::getline(std::cin, input);
        if(input == "0")
            break;
        tempITM.itemName = input;
        std::cout << "Input the price for the item[" << count+1 << "]t:t";
        std::getline(std::cin, input);
        tempSTRM.str(input);
        tempSTRM >> tempPrice;
        tempITM.price = tempPrice;
        menu.items.push_back(tempITM);
        tempITM = *itemITR; //this line is causing the problem
        std::cout << tempITM.itemName << "t" << tempITM.price << "n";
        itemITR++;
        count++;
    }while(input !="0");
    return false;
}

2.菜单结构

struct MenuItems
{
    std::string menuName;
    std::vector <Item> items;
};
typedef struct MenuItems Menu;

3.项目结构

    struct Item
{
    std::string itemName;
    int price;
    bool promoStatus;
    double promoDiscount;
};
typedef struct Item Item;

4 Main(只有代码的这一部分与我现在正在构建的内容有关)

#include <windows.h> //leftover code from previous experiment
#include <tchar.h>   //leftover code from previous experiment
#include "CoffeeShop.h"
using namespace std;
int main()
{
    Shop* CoffeeShop = new Shop();
//Other leftover codes
    return 0;
}

我的意图是:获得我试图推动的任何东西的输出(因为我正在构建项目,所以一旦其他部分准备好,我就可以自信地产生输出。)

由于这会引发SIGSEGV问题,所以我目前的想法行不通。尝试过放置指针对象Item并访问itemITR,但也不起作用。任何人都可以揭露我(在意识形态上)做错了什么,我应该怎么做?

--编辑--

Shop::Shop()
{
    if(!initShop())
        std::cout << "Object creation succeeded!n";
    else
        std::cout << "Object creation failed!n";
}
bool Shop::initShop()
{
    if(!initMenu())
    {
        std::cout << "Object creation succeeded!n";
        return false;
    }
    else
    {
        std::cout << "Object creation failed!n";
        return true;
    }
}
bool Shop::initMenu()
{
    if(!initItem())
    {
        std::cout << "Object Creation succeeded!n";
        return false;
    }
    else
    {
        std::cout << "Object creation failed!n";
        return true;
    }
}

流程->Shop构造函数调用initShop,然后initShop调用initMenu,initMenu调用initItem

商店->initShop->initMenu->initItem

--编辑2--
已解决。增加

itemITR = menu.items.begin();

线后

menu.items.push_back(tempITM);

谢谢!

迭代器是用给定的向量初始化的:

std::vector <Item>::iterator itemITR = menu.items.begin();

然后在向量中插入一些东西:

    menu.items.push_back(tempITM);

这可能会使迭代器无效。因此,取消引用它是UB,并在此处导致segfault