繁体   English   中英

为什么我的字符串数组的 class 突变体 function 导致进程返回 -1073741819 (0xc0000005)

[英]Why does my class mutator function for a string array cause process returned -1073741819 (0xc0000005)

我正在为 class 作业开发一个程序,该程序读取一个输入文件,该文件详细说明了开放国际象棋的移动及其统计数据。 稍后,程序应该将“opening.txt”中的开局动作与示例游戏“game1.txt”中的动作进行比较,以查看它是否可以检测到开局动作。 好吧,我还没有过去尝试读取输入文件“opening.txt”。 我遇到的问题是 setMoves() 导致我的程序返回进程返回 -1073741819 (0xc0000005)。 我无法弄清楚我的 Opening.h、Opening.cpp 和 main.cpp 中 ReadOpenings function 中的 setMoves() 调用之间有什么问题。 分配说明说我必须将字符串 move[6] 声明为 class 中的私有成员。 然后我需要有访问器和修改器功能。 我当前的代码是我没有得到编译器错误的唯一方法,但它仍然不起作用。

#include <iostream>
#include <fstream>
#include "Opening.h"
#include "Game.h"
#include <stdlib.h>
#include <iomanip>

using namespace std;

int GetNumOpenings();
void CompareOpenings(Opening openings[], int numOpenings);
string DetectOpening(Game *game, Opening opening);
string GetString();
void Menu();
void ReadOpenings(Opening arr[], int size);
void PrintOpenings(Opening arr[], int size);

int main()
{
    Menu();
    return 0;
}

void Menu(){

    int numOpenings = 0;
    numOpenings = GetNumOpenings();
    Opening openings[numOpenings];
    ReadOpenings(openings, numOpenings);

    while(true){
        int action;
        cout<<"Print Stored Openings (1)"<<endl;
        cout<<"Load  Game   File     (2)"<<endl;
        cout<<"Exit                  (0)"<<endl<<endl;

        cout<<"Enter action:";
        cin>>action;
        cin.ignore();
        cout<<endl;
        switch(action){
        case 0:
            cout<<"Exit Selected"<<endl;
            break;
        case 1:
            PrintOpenings(openings, numOpenings);
            break;
        case 2:
            CompareOpenings(openings, numOpenings);
            break;
        default:
            cout<<"Invalid Action"<<endl;

        }
        if(action == 0)
            break;
    }
}

string GetString(){
    string filename;
    cout<<"Please enter game filename:";
    cin>>filename;
    cin.ignore();
    return filename;
}

int GetNumOpenings(){
    ifstream infile;
    infile.open("openings.txt");
    int numOpenings;
    infile>>numOpenings;
    infile.close();
    return numOpenings;
}

void CompareOpenings(Opening openings[], int numOpenings){

    //FILL IN
}

string DetectOpening(Game *game, Opening opening){

   //FILL IN
   //remove this return statement
    return " ";
}

void PrintOpenings(Opening arr[], int size){

    //FILL IN
string nameT;
string movesT[6];
string opening_countT;
double wwpT;
double dpT;
double bwpT;

cout<< left << setw(40)<<"NAME"
    << left << setw(30)<<"MOVES"
    << left << setw(30)<<"WIN PERCENTAGES"
    << left << setw(15)<<"TIMES PLAYED" << endl<<endl;
    for(int i=0; i<7; i++){
nameT = arr[i].getName();
cout <<left << setw(40)<< nameT;
cout << left <<setw(30)<< arr[i].getMoves();
wwpT = arr[i].getWWP();
cout << wwpT<<",";
dpT = arr[i].getDP();
cout <<dpT<<",";
bwpT = arr[i].getBWP();
cout <<left<< setw(30)<< bwpT;
opening_countT = arr[i].getOpening_Count();
cout<<left << setw(15)<< opening_countT<<endl;
    }
}

void ReadOpenings(Opening arr[], int size){

    //FILL IN
string nameT;
int counter;
string movesT[6] = {"","","","","",""};
string opening_countT;
double wwpT;
double dpT;
double bwpT;

 ifstream infile;
 infile.open("openings.txt");
 infile.ignore(100, '\n');
 for(int i=0; i < 7; i++){
 getline(infile, nameT);

 infile>> counter;
    for(int j=0;j<counter;j++){
    infile>>movesT[j];
        }
 infile>>opening_countT>>wwpT>>dpT>>bwpT;

 arr[i].setName(nameT);
 arr[i].setMoves(movesT);
 arr[i].setOpening_Count(opening_countT);
 arr[i].setWWP(wwpT);
 arr[i].setDP(dpT);
 arr[i].setBWP(bwpT);
 infile.ignore(100, '\n');


    }

infile.close();
}

打开.h

#ifndef OPENING_H
#define OPENING_H
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <array>

using namespace std;

class Opening{

    public:
    Opening(); ///constructor



    ///accessor functions
    string getName(){return name;}
    string getMoves(){return moves[6];}
    string getOpening_Count(){return opening_count;}
    double getWWP(){return white_win_percent;}
    double getDP(){return draw_percent;}
    double getBWP(){return black_win_percent;}

    ///mutator functions
    void setName(string nameT){ name = nameT;}
    void setMoves(string movesT[6]){ moves[6] = movesT[6]; }
    void setOpening_Count(string opening_countT){opening_count = opening_countT;}
    void setWWP(double wwpT){white_win_percent = wwpT;}
    void setDP( double dpT){draw_percent = dpT;}
    void setBWP(double bwpT){black_win_percent = bwpT;}


    private:
    string name;
    string moves[6];
    string opening_count;
    double white_win_percent;
    double draw_percent;
    double black_win_percent;



};




#endif // OPENING_H

打开.cpp

#include "Opening.h"
#include <iostream>
Opening::Opening(){

 name="";
string moves[6];
opening_count="1";
 white_win_percent = 0;
 draw_percent = 0;
 black_win_percent = 0;

}

开口.txt

7
King's Gambit
    3
    e4 e5 f4 
    4055
    31 32 37
Italian Game
    3
    e4 e5 Bc4
    6712
    36 40 24
Bishop's Opening
    4
    e4 e5 Bc4 Nc6
    955
    39 36 26
Ruy Lopez
    5
    e4 e5 Nf3 Nc6 Bb5
    108725
    31 50 20
English Opening Anglo-Dutch Defense
    3
    c4 f5 Kc3
    1021
    37 37 26
Canard Opening
    6
    d4 Nf6 f4 d5 Nf3 Kd7
    1
    0 0 100
Zukertort Opening Sicilian Invitation
    3
    Nf3 c5 c4
    16039
    31 45 23

这是一个明显的错误

void setMoves(string movesT[6]){ moves[6] = movesT[6]; }

movesT moves是大小为 6 的 arrays ,因此 move moves[6]是越界数组访问,导致程序崩溃。 moves的合法索引是从 0 到 5(含)。

显然你想要做的是复制整个数组,但你应该写一个循环来做到这一点(或者你可以使用std::copy函数)

void setMoves(string movesT[6]) {
    for (int i = 0 i < 6; ++i)
        moves[i] = movesT[i];
}

顺便说一句,你在这里有同样的错误

string getMoves(){return moves[6];}

由于完全相同的原因,这是错误的。

您应该考虑使用向量而不是 arrays。 它们比 arrays 更简单、更强大。 Arrays 在 C++ 中相当损坏。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM