任何想法如何使用以下代碼修復以下錯誤?這是在Xcode的C++環境中。調試ECX_BAD_ACCESS錯誤C++
//
// main.cpp
// Chess
//
// Created by Akshar Ramkumar on 9/29/16.
// Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#include "Declarations.hpp"
#include "DeclarationsMain.hpp"
extern simplePiece TotalBoard[8][8];
using namespace std;
int main() {
Piece All[32];
start(All);
char rawinput[4];
char fxinput;
char fyinput;
char txinput;
char tyinput;
while(true){
Turn=not Turn;
if(Turn==true){
TurnColor="white";
}
else {
TurnColor="black";
}
cout<<"It is "<<TurnColor<<"'s turn"<<endl;
cout<<"Enter the x and y coordinates you want to move from 0-7, 0-7 (no spaces in between)"<<endl;
cin>>rawinput;
fxinput=rawinput[0];
fyinput=rawinput[2];
cout<<"Enter the x and y coordinates you want to move to 0-7, 0-7 "<<endl;
cin>>rawinput;
txinput=rawinput[0];
tyinput=rawinput[2];
if(TotalBoard[fxinput][fyinput].Color==Turn and TotalBoard[fxinput][fyinput].Color==true){
TotalBoard[txinput][tyinput]=TotalBoard[fxinput][fyinput];
TotalBoard[fxinput][fyinput].Color=false;
TotalBoard[fxinput][fyinput].Type=0;
TotalBoard[fxinput][fyinput].exists=false;
}
}
return 0;
}
下一頁:
//
// Classes.cpp
// Chess
//
// Created by Akshar Ramkumar on 10/13/16.
// Copyright © 2016 Akshar Ramkumar. All rights reserved.
//Pawn = 0
//Rook = 1
//Knight = 2
//Bishop = 3
//King = 4
//Queen = 5
#include "Declarations.hpp"
#include "DeclarationsBoard.hpp"
void start(Piece All[32]){
int TypeArray[32];
int xValues[32];
int yValues[32];
std::ifstream startstate;
std::fstream boardstate;
std::string numread;
char delim=' ';
startstate.open("/Users/aksramk/Google Drive/For Fun/Programming/Pascal C++/Chess/boardstatestart.txt")
;
boardstate.open("/Users/aksramk/Google Drive/For Fun/Programming/Pascal C++/Chess/boardstatecurrent.txt");
for(int i=0;i<32;i++){
getline(startstate, numread,delim);
TypeArray[i]=atoi(numread.c_str());
}
for(int i=0;i<32;i++){
getline(startstate, numread, delim);
xValues[i]=atoi(numread.c_str());
}
for(int i=0;i<32;i++){
getline(startstate, numread, delim);
yValues[i]=atoi(numread.c_str());
}
for (int i=0;i<32;i++){
All[i].Type = TypeArray[i];
All[i].y = yValues[i];
All[i].x = xValues[i];
All[i].Color = true;
All[i].Captured = false;
if (i>15){
All[i].Color = false;
}
}
startstate.close();
for(int i=0;i<32;i++){
boardstate<<TypeArray[i];
boardstate<<" ";
}
for(int i=0;i<32;i++){
boardstate<<xValues[i];
boardstate<<" ";
}
for(int i=0;i<32;i++){
boardstate<<yValues[i];
boardstate<<" ";
}
for(int i=0;i<32;i++){
boardstate<<0;
boardstate<<" ";
}
boardstate.close();
;
for(int i=0;i<8;i++){
for(int j=0;j<8;j++){
simplePiece temp;
for(int k=0;k<32;k++){
Piece temp2=All[k];
if(temp2.x==i and temp2.y==j){
temp.Type=temp2.Type;
temp.Color=temp2.Color;
temp.exists=true;
}
}
TotalBoard[i][j]=temp;
}
}
};
接頭:
// DeclarationsMain.h
// Chess
//
// Created by Akshar Ramkumar on 11/22/16.
// Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#ifndef DeclarationsMain_h
#define DeclarationsMain_h
bool Turn=false;
std::string TurnColor;
#endif /* DeclarationsMain_h */
下一頁:
//
// DeclarationsBoard.h
// Chess
//
// Created by Akshar Ramkumar on 11/22/16.
// Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#ifndef board
#define board
simplePiece TotalBoard[8][8];
#endif
下一頁:
//
// DataStructures.hpp
// Chess
//
// Created by Akshar Ramkumar on 10/18/16.
// Copyright © 2016 Akshar Ramkumar. All rights reserved.
//
#ifndef iostream
#define iostream
#include <iostream>
#include <fstream>
#endif
#ifndef piece
#define piece
class Piece {
public:
int Type;
int x;
int y;
bool Captured;
bool Color;
};
struct simplePiece{
bool Color=false;
int Type=0;
bool exists=false;
};
void start(Piece All[32]);
#endif
線:
TotalBoard[txinput][tyinput]=TotalBoard[fxinput][fyinput];
引發錯誤:「線程1:ECX_BAD_ACCESS(代碼= 2,地址= 0x100009bac)。
有沒有人有任何關於如何解決這個問題的想法?
非常感謝,對於簡單的問題感到抱歉(我是初學者)。
,並檢查什麼的價值觀fxinput,fyinput,txinput和tyinput在將它們的類型改爲'int'之後,我相當肯定你會自己弄清楚自己的bug。自由線索:當你輸入座標0到7,被輸入到這些變量中時,這些變量的實際值將不會是0到7. –
我已經試過了。 TotalBoard似乎不存在於main.cpp的範圍內。 –
這與main.cpp中這四個變量的值有什麼關係?你讀過我的評論,還是別人的? –