为什么我的字符串数组的类更改器函数导致进程返回-1073741819(0xc0000005) [英] Why does my class mutator function for a string array cause process returned -1073741819 (0xc0000005)
问题描述
我正在为一个用于班级分配的程序工作,该程序读取一个输入文件,该文件详细说明了开局象棋动作及其状态.稍后,程序应将"opening.txt"中的开局动作与示例游戏"game1.txt"中的动作进行比较,以查看其是否可以检测到开局动作.好吧,我还没有尝试读取输入文件"opening.txt".我遇到的问题是setMoves()导致我的程序返回返回-1073741819(0xc0000005)的进程.我无法弄清楚我的Opening.h,Opening.cpp和main.cpp的ReadOpenings函数中对setMoves()的调用之间有什么问题.赋值指令说我必须将字符串move [6]声明为类中的私有成员.然后,我需要具有访问器和更改器功能.我当前的代码是我不会遇到编译器错误的唯一方法,但仍然无法正常工作.
I'm working on a program for a class assignment that reads an input file which details opening chess moves and their stats. Later the program should compare opening moves from "opening.txt" to moves in a sample game "game1.txt" to see if it can detect the opening moves. Well I haven't gotten past trying to read in the input file "opening.txt". The problem I have is setMoves() causes my program to return process returned -1073741819 (0xc0000005). I can't figure out what's wrong between my Opening.h, Opening.cpp, and the call for setMoves() in the ReadOpenings function in main.cpp. The assignment instructions say I have to declare string moves[6] as a private member in the class. Then I need to have accessor and mutator functions. My current code is the only way I don't get compiler errors but it still doesn't work.
#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();
}
Opening.h
Opening.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
Opening.cpp
Opening.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;
}
Openings.txt
Openings.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]; }
moves
(和movesT
)是大小为6的数组,因此moves[6]
是超出范围的数组访问,导致程序崩溃. moves
的合法索引范围是0到5.
moves
(and movesT
) are arrays of size 6, so moves[6]
is an out of bounds array access, resulting in a crash of your program. The legal indexes for moves
are from 0 to 5 inclusive.
很显然,您要复制整个数组,但是您应该编写一个循环来做到这一点(或者您可以使用std::copy
函数)
Obviously what you are trying to do is copy the whole array, but you should write a loop to do that (or you could use the std::copy
function)
void setMoves(string movesT[6]) {
for (int i = 0 i < 6; ++i)
moves[i] = movesT[i];
}
顺便说一句,您在这里有相同的错误
Incidentally you have the same error here
string getMoves(){return moves[6];}
出于完全相同的原因这是错误的.
which is wrong for exactly the same reason.
您应该研究使用向量而不是数组.它们比数组更容易,功能也更强大.数组在C ++中相当复杂.
You should look into using vectors instead of arrays. They are easier and more powerful than arrays. Arrays are rather broken in C++.
这篇关于为什么我的字符串数组的类更改器函数导致进程返回-1073741819(0xc0000005)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!