簡體   English   中英

分段錯誤(核心轉儲)C ++

[英]Segmentation Fault (core dumped) C++

經過研究,我相信我理解"Segmentation fault"錯誤是什么。 然而,即使在逐行注釋掉代碼之后,我似乎無法找到故障發生的位置以便修復它。 我有什么東西可以忽略導致這個錯誤嗎? 以下是我運行代碼時顯示的內容:

准備玩了(是/否)? ÿ

3C AH 4C 3H 4H 2H 5H 2C 5C AC

這是你的卡片:3C AH 4C 3H 4H

分段故障(核心轉儲)

下面粘貼的是我所指的代碼。 注釋掉的部分只是我試圖找出錯誤發生的位置:

#include <iostream>
#include <vector>
#include <algorithm>
#include <sstream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;

vector<string> bigDeck;
vector<string> cardDeck;
vector<string> playerHand;
vector<string> computerhand;
vector<string> shortvec;

const int DEAL_CARDS = 5;


void ResetDeck();
void Shuffle();
void DealACard();
void DealQCard();
void DealBCard();
string CStr(int);
int letsee2;

int main()
{


cout << "Ready to play (y/n)? ";

char yn;
cin >> yn;
if (yn == 'n' || yn != 'y') return 0;

ResetDeck();

srand(time(0));

Shuffle();

for (int f=0; f < 10; f++)
{
        cout << cardDeck[f] << " ";
}


cout << "\n\nHere's your cards: ";
for (int i=0; i < DEAL_CARDS; i++)
{
        DealACard();
        cout << playerHand[i] << " ";
}

cout<<"\n\nHere's the Computer's cards: ";
for (int k=0; k < DEAL_CARDS; k++)
{
        DealBCard();
        cout << computerhand[k] << " ";
}

for (int u=0; u < DEAL_CARDS; u++)
{
        DealQCard();
}

cout<<shortvec.size()<<endl;

cout<<endl<<endl;

//do
//{

for (int woh=0; woh < DEAL_CARDS; woh++)
{
 if ((playerHand[woh][0]=='A') && (computerhand[woh][0]=='A'))
        {
                cout<<"War!"<<endl;


        }
        else if ((playerHand[woh][0]=='A') && (computerhand[woh][0]!='A'))
        {
                cout<<"Player wins"<<endl;
                /*playerHand.push_back(computerhand[woh]);
                computerhand.erase(computerhand.begin()+(woh-1));*/
        }
        else if ((playerHand[woh][0]!='A') && (computerhand[woh][0]=='A'))
        {
                cout<<"Computer Wins"<<endl;
                /*computerhand.push_baci(playerHand[woh]);
                playerHand.erase(playerHand.begin()+(woh-1));*/
        }
        else
        {
                if ((atoi(playerHand[woh].c_str())) > (atoi(computerhand[woh].c_str())))
                {
                        cout<<"Player wins!"<<endl;
                        /*playerHand.push_back(computerhand[woh]);
                        computerhand.erase(computerhand.begin()+(woh-1));*/
                }
                else if ((atoi(playerHand[woh].c_str())) < (atoi(computerhand[woh].c_str())))
                {
                        cout<<"Computer wins!"<<endl;
                        /*computerhand.push_back(playerHand[woh]);
                        playerHand.erase(playerHand.begin()+(woh-1));*/
                }
                else
                {
                        cout<<"War!"<<endl;

                }
        }
/*if (playerHand.size() > computerhand.size())
        shortvec = computerhand;
else
        shortvec = playerHand;

cout<<endl<<endl;
*/
}
/*for (int z=0; z < playerHand.size(); z++)
{
        cout << playerHand[z] << " ";
}

cout<<"\n\nHere's the Computer's cards: ";
for (int y=0; y < computerhand.size(); y++)
{
        cout << computerhand[y] << " ";
}*/

cout<<endl<<endl;
//}
//while(((playerHand.size())!=10) || (computerhand.size())!=10);

return 0;
}
void Shuffle()
{
        random_shuffle(cardDeck.begin(),cardDeck.end());
}

void DealBCard()
{
        computerhand.push_back(cardDeck[0]);
        cardDeck.erase(cardDeck.begin());
}

void DealACard()
{
        playerHand.push_back(cardDeck[0]);
        cardDeck.erase(cardDeck.begin());
}

void DealQCard()
{
        shortvec.push_back(bigDeck[0]);
        bigDeck.erase(bigDeck.begin());
}

string CStr(int n)
{
        stringstream s;
        s << n;
        return s.str();
}

void ResetDeck()
{
        cardDeck.clear();
        playerHand.clear();
        computerhand.clear();

        for (int i=2; i<6; ++i)
        {
                cardDeck.push_back(CStr(i) + "H");
                cardDeck.push_back(CStr(i) + "C");
        }
        cardDeck.push_back("AH");
        cardDeck.push_back("AC");
}

你有一個名為bigDeckstd::vector ,在DealQCard你嘗試訪問它的第0個元素,盡管它沒有元素。 你的意思是在bigDeck放一些卡嗎?

嘗試訪問其他進程正在使用的不存在的內存或內存也會導致分段錯誤(核心轉儲)。

核心轉儲意味着已經記錄了程序狀態,即其內存和處理器中的資源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM