C++ OOP 分段错误

C++ OOP segmentation fault

本文关键字:错误 分段 OOP C++      更新时间:2023-10-16

我在学院有一项任务。我正在制作一个棋盘,上面有棋子。我有一种step()移动它们的方法。但是我需要打印移动的结果。尝试打印后出现分段错误。并且有没有其他方法可以声明带有符号的棋盘。在我看来,它没有正确声明。(是的,棋子应该像"01""10".稍后我将使用它来为每种作品类型制定规则(。

#include <iostream>
#include <stdio.h>
#include <string>
#define n 8
using namespace std;
int convert(char c) {
if(c=='a')
return 0;
else if(c=='b')
return 1;
else if(c=='c')
return 2;
else if(c=='d')
return 3;
else if(c=='e')
return 4;
else if(c=='f')
return 5;
else if(c=='g')
return 6;
else 
return 7;
}
class chess {
private:
string board[n][n]= {{"02","03","04","06","05","04","03","02"}, //chessboard
{"01","01","01","01","01","01","01","01"}, //black
{"00","00","00","00","00","00","00","00"},
{"00","00","00","00","00","00","00","00"},
{"00","00","00","00","00","00","00","00"}, //00-empty cells
{"00","00","00","00","00","00","00","00"},
{"11","11","11","11","11","11","11","11"}, //white
{"12","13","14","16","15","14","13","12"}};
public:
chess() {}
void print() {
cout<<"  _";  //board marking
for (int i='a'; i<='h'; i++)
printf("_%c_",i);
cout<<endl;
for (int i=0; i<n; i++) { 
printf("%d| ", i+1); //board marking
for (int j=0; j<n; j++) {
printf("%.2s ",board[i][j].c_str()); //print chess pieces
}
cout<<'n';
}
cout<<'n';
}
void step() { //move on another cell
char figc, fign, cellc, celln;
cout<<"Piece, char: "; //choose a chess piece
cin>>figc;
cout<<"Piece, num: ";
cin>>fign;
cout<<"Step, char: "; //move on cell - ...
cin>>cellc;
cout<<"Step, num: ";
cin>>celln;
board[celln-1][convert(celln)]=board[fign-1][convert(fign)];
board[fign-1][convert(fign)]="00";
}
};
int main() {
chess A;
A.step();
A.print();
return 0;
}

例。截图

编译代码

g++ -g -faddress=sanitize main.cpp

显示以下输出:

#0 0x7fdc8e7c4b40 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x124b40)
#1 0x7fdc8e7c4f18 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x124f18)
#2 0x56310c7ad092 in chess::step() /z/bob.cpp:65
#3 0x56310c7a9c10 in main /z/bob.cpp:72
#4 0x7fdc8e0b8b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
#5 0x56310c7a9a19 in _start (/z/a.out+0x1a19)

第 65 行是:

board[fign-1][convert(fign)]="00";

在其中我们看到fign,这是一个用作整数的char

由于字母和数字的 ascii 范围在 48-122 之间,这超出了数组的大小。

board.at(fign-1).at(convert(fign))="00";

会立即告诉你这一点,因为.at()会进行边界检查。

  • 你应该fign & celln数据类型作为int.
  • 而且您还需要有一些条件,检查输入字符 (a-z(,而且数字也是< n.

    #include <iostream>
    #include <stdio.h>
    #include <string>
    #define n 8
    using namespace std;
    int convert(char c) {
    if(c=='a')
    return 0;
    else if(c=='b')
    return 1;
    else if(c=='c')
    return 2;
    else if(c=='d')
    return 3;
    else if(c=='e')
    return 4;
    else if(c=='f')
    return 5;
    else if(c=='g')
    return 6;
    else 
    return 7;
    }
    class chess {
    private:
    string board[n][n]= {{"02","03","04","06","05","04","03","02"}, //chessboard
    {"01","01","01","01","01","01","01","01"}, //black
    {"00","00","00","00","00","00","00","00"},
    {"00","00","00","00","00","00","00","00"},
    {"00","00","00","00","00","00","00","00"}, //00-empty cells
    {"00","00","00","00","00","00","00","00"},
    {"11","11","11","11","11","11","11","11"}, //white
    {"12","13","14","16","15","14","13","12"}};
    public:
    chess() {}
    void print() {
    cout<<"  _";  //board marking
    for (int i='a'; i<='h'; i++)
    printf("_%c_",i);
    cout<<endl;
    for (int i=0; i<n; i++) { 
    printf("%d| ", i+1); //board marking
    for (int j=0; j<n; j++) {
    printf("%.2s ",board[i][j].c_str()); //print chess pieces
    }
    cout<<'n';
    }
    cout<<'n';
    }
    void step() { //move on another cell
    char cellc, figc;
    int fign,celln; //change datatype as int
    cout<<"Piece, char: "; //choose a chess piece
    cin>>figc;
    cout<<"Piece, num: ";
    cin>>fign;
    cout<<"Step, char: "; //move on cell - ...
    cin>>cellc;
    cout<<"Step, num: ";
    cin>>celln;
    // cout<<"print=>"<<convert(cellc)<<endl;
    // cout<<celln-1<<"  "<<convert(cellc)<<endl;
    // cout<<fign-1<<"  "<<convert(figc)<<endl;
    // cout<<board[celln-1][convert(cellc)].c_str();
    // cout<<board[fign-1][convert(figc)].c_str();
    board[celln-1][convert(cellc)]=board[fign-1][convert(figc)];
    board[fign-1][convert(figc)]="00";
    }
    };
    

你正在使用一个字符fign并在其上做-1。

在这种情况下最好使用 atoi --

board[atoi(&fign)-1][convert(fign)]="00";

输出--

__a__b__c__d__e__f__g__h_
1| 02 03 04 06 05 04 03 02
2| 01 01 01 01 01 01 01 01
3| 00 00 00 00 00 00 00 00
4| 00 00 00 00 00 00 00 00
5| 00 00 00 00 00 00 00 00
6| 00 00 00 00 00 00 00 00
7| 11 11 11 11 11 11 11 00
8| 12 13 14 16 15 14 13 12

为什么你不应该执行字符输入?

好吧,我在那个上面添加了一个 printf %d,给了我这个,这是不言自明的。

.
printf ("nThe result of bad operation : %dn", fign - 1);
.
.
The result of bad operation : 54