2014-03-29 64 views
0

因此,我正在製作一個連接4遊戲,可以在任何大小的板上連接任何數量的遊戲,並使用ai與您對戰。目前,它不能正確顯示電路板,我不相信電路板正在正確創建。另一個問題是當我真的嘗試放置第一枚硬幣時,我得到了一個突破錯誤。 (錯誤消息:「連接[x,n] .exe中0x00195832的第一次機會異常:0xC0000005:訪問衝突寫入位置0x5A5A5A57。」併發生在我嘗試設置陣列的「頂部」位置的電路​​板的添加函數中一個玩家/ AI硬幣。在C++中導致問題的動態二維數組

總體來說,我只是不使用C++使用2D動態數組妥善認爲,即時通訊,並希望有人能看到我的錯誤,在這裏

我的「主」 CPP

// Connect[x,n].cpp : Defines the entry point for the console application. 
// 

#include "stdafx.h" 
#include "time.h" 
#include <string> 
#include <iostream> 
#include "windows.h" 
#include "winioctl.h" 
#include <cstdlib> 
#include "AIConnectBoard.h" 


using namespace std; 





int main(int argc, char* argv[]) 
{ 
    char again; 
    int tm, tn, conNum; 
    char aiturn; 

do{ 

    srand((unsigned)time(NULL)); //random seed based on time 
    cout << "Board Width:"; 
    cin >> tn; 
    cout << "Board Height:"; 
    cin >> tm; 
    cout << "Connect?"; 
    cin >> conNum; 
    cout << "AI moves first?(y/n)"; 
    cin >> aiturn; 


    AIConnectBoard *b = new AIConnectBoard(); 
    b->setBoard(tm, tn); 
    b->setFirst(aiturn); 


    while (b->checkwin()==0) //keep asking for moves until someone wins 
    { 
     b->dispBoard(); 
     if (b->endt == 0) 
     { 
      int coincol; 
      if (b->turn == b->plyr) 
      { 
       do 
       { 
       cout << "What column?" << "(1"<< "-" << tn << ")" << '\n'; 
       cin >> coincol; 

       if ((coincol > tn) || (coincol < 1)) 
       { 
        cout << "That is not an accepted value, please enter a number between 1 and " << tn <<'\n'; 
       } 

       }while ((coincol > tn) || (coincol < 1)); 
       coincol--; 
      } 
      if (b->add(coincol, b->plyr) == 0) 
      { 
       if (b->endt == 0) 
       { 
        if (b->add(coincol, b->plyr) == 0) 
        { 
         b->lin = 6 - b->top[coincol]; //place players choice 
         b->checkturn(); 
         b->turn = b->ai; 

         if (b->endt == 0) 
         { 
          int t; 
          b->col = b->Think(); 
          t = b->add(b->col, b->ai); 
          if (t == 0) 
          { 
           b->lin = 6 - b->top[b->col]; //place ai's choice 
           b->turn = b->plyr; 
          } 
         } 
        } 
       } 
      } 
     } 
    } 

    cout << "Play Again? (y/n)"; 
    cin >> again; 
} while (again != 'n'); 
return 0; 
}; 

my aiconnectboard.cpp

#include "stdafx.h" 
#include "AIConnectBoard.h" 
#include "stdafx.h" 
#include "time.h" 
#include <string> 
#include <iostream> 
#include "windows.h" 
#include "winioctl.h" 
#include <cstdlib> 

using namespace std; 

//constructor 
AIConnectBoard::AIConnectBoard(){ 
endt = 0; 
turn = 1; 
} 

//destructor 
AIConnectBoard::~AIConnectBoard() 
{ 

} 

//create board to m x n specifications 
void AIConnectBoard::setBoard(const int tn, const int tm) 
{ 
n = tn; 
m = tm; 
noPlayer = 0; 
plyr = 1; 
ai = 2; 
int** arry = 0; 
arry = new int*[n]; 
for (int i = 0; i < n; i++) //make board 2d && make empty 
{ 
    arry[i] = new int[m]; 
    arry[i][m] = noPlayer; 
} 

int** thnk = new int*[m, n]; 
int* top = new int[n]; 
for (int i = 0; i < m; i++) //set top to bottom of board 
{ 
    top[i] = 0; 
} 
return; 
} 

//sets first plyr/turn. Red moves first, then Yellow 
void AIConnectBoard::setFirst(char a) 
{ 
if (a == 'y') //ai goes first 
    { 
     turn=ai; 
     aiColor= 'R'; 
     plyrColor= 'Y'; 
    } 
else //plyr goes first 
{ 
    turn=plyr; 
    aiColor= 'Y'; 
    plyrColor = 'R'; 
} 
} 

//output board 

*******GETS OUPUT AS 2 ROWS OF 0s AND DOESNT FILL EVERYTHING 

void AIConnectBoard::dispBoard() 
{ 
for (int x=1;x<=m;x++) //rows on top of board 
{ 
    cout << x << " "; 
} 
cout << '\n'; 
for (int i = 0; i < m; i++) //display current state of board 
{ 
    for (int j = 0; j < n; j++) 
    { 
     if (arry[i,j] == (int*)1) 
     { 
      cout << plyrColor << " "; 
     } 
     if (arry[i,j] == (int*)2) 
     { 
      cout << aiColor << " "; 
     } 
     else if (arry[i,j]==(int*)0) 
     { 
      cout << "O "; 
     } 
    } 
    cout << '\n'; 
} 
} 

//AI starts thinking--set difficulty here 
int AIConnectBoard::Think() //levels of recursion 
{ 
int i = (m*n)/7; //will give 6 levels of recursion on 7x6 
i = recur; 
return check(i); 
} 

//check for an end to game (win/lose/draw) 
void AIConnectBoard::checkturn() 
{ 
int temp; 
temp = checkwin(); 
if (temp == plyr) 
{ 
    cout << "You win!" << '\n'; 
    endt=1; 
} 
else if (temp == ai) 
{ 
    cout << "You lose!" << '\n'; 
    endt=1; 
} 
else if (temp == 0) 
{ 
    for (t = 0; t < n; t++) 
    { 
     if (n - top[t] < 0) 
     { 
      temp = 1; 
     } 
    } 
    if (temp == 0) 
    { 
     endt = 1; 
     return; 
    } 
} 
} 

//1st AI check score of move function 
int AIConnectBoard::check(int i) 
{ 
int co, score, t, g, j = 0, p; 
i--; 
if (i == -1) 
{ 
    score = position(); 
    return score; 
} 

if (i % 2 == 0) 
{ 
    int max = 0, k; 
    int j = 0; 
    int co = 0; 
    for (t = 0; t < n; t++) 
    { 
     g = add(t, ai); 
     if (g == 0) 
     { 
      if (checkwin() == ai) 
      { 
       sub(t); 

       if (i == recur - 1) 
       { 
        return t; 
       } 
       else 
       { 
        return 9000; 
       } 
      } 
      k = check(i); 

      if (co == 0) 
      { 
       max = k; 
       co = 1; 
       j = t; 
      } 

      if (k == max) 
      { 
       p = rand() % m + 1; 
       if (p > 4) 
       { 
        j = t; 
       } 
      } 

      if (k > max) 
      { 
       max = k; 
       j = t; 
      } 
      sub(t); 
     } 
     score = max; 
    } 
} 
else 
{ 
    int min = 0, k = 0; 
    co = 0; 
    for (t = 0; t < m; t++) 
    { 
     g = add(t, plyr); 
     if (g == 0) 
     { 
      if (checkwin() == plyr) 
      { 
       sub(t); 
       return -10000; 
      } 
      k = check(i); 
      if (co == 0) 
      { 
       min = k; co = 1; j = t; 
       if (k < min) 
       { 
        min = k; 
        j = t; 
       } 
      } 
      sub(t); 
     } 
     score = min; 
    } 
} 
if (i == recur - 1) 
{ 
    return j; 
} 
return score; 
} 


//add a coin to the specified c column (g for ai vs plyr) 
int AIConnectBoard::add(int c, int g) 
{ 
int* coin; 
int tcoin = 0; 
coin = &tcoin; //initialize coin to something 

//set coin to be ai or player 
if (g == plyr) 
{ 
    coin = &plyr; 
} 
if (g == ai) 
{ 
    coin = &ai; 
} 
//if not at top of board, drop coin and increase top array 
if (top[c] < n) 
{ 
    if (g == plyr) 
    { 
     arry[c][top[c]] = plyr; *******WHERE I GET THE EXCEPTION ERROR (ASSUMING IM USING A NULL POINTER OR SOMETHING, BUT CANT TELL WHERE) 
     top[c]++; 
    } 
    else if (g == ai) 
    { 
     arry[c][top[c]] = ai; 
     top[c]++; 
    } 
    return 1; 
} 
} 



//remove a coin from the specified c column 
int AIConnectBoard::sub(int c) 
{ 
top[c]--; 
arry[c, top[c] = 0]; 
return 0; 
} 

//keeps track of where AI is currently contemplating a move 
int AIConnectBoard::position() 
{ 
int u, z, x, y, j, score; 
int gh = 0, hg = 0; 
score = 0; 

//Empty Think 
for (x = 0; x < m; x++) 
{ 
    for (y = 0; y < n; y++) 
    { 
     thnk[x, y] = 0; 
    } 
} 
for (y = 0; y < n; y++) 
{ 
    for (x = 0; x < m; x++) 
    { 
     if (arry[x, y] == 0) 
     { 
      score += checkhole(x, y); 
     } 
     if (y>0) 
     { 
      if (thnk[x, y] == (int*)ai && arry[x, y - 1] != 0) 
      { 
       gh++; 
      } 
      if (thnk[x, y] == (int*)plyr && arry[x, y - 1] != 0) 
      { 
       hg++; 
       score = score - 4000; 
      } 
     } 
    } 
} 

if (gh > 1) 
{ 
    score += (gh - 1) * 500; 
} 
if (gh == 1) 
{ 
    score = score - 100; 
} 
if (hg > 1) 
{ 
    score -= (hg - 1) * 500; 
} 

for (x = 0; x < m; x++) 
{ 
    gh = 0; 
    for (y = 1; y < 6; y++) 
    { 
     if (thnk[x, y] == (int*)ai && arry[x, y - 1] == (int*)ai) 
     { 
      u = 0; j = 0; 
      for (int z = y - 1; 0>-1; z--) 
      { 
       if (thnk[x, z] == (int*)plyr) 
       { 
        u = 1; 
       } 
       if (arry[x, z] == 0) 
       { 
        j++; 
       } 
      } 
      if (u == 0) 
      { 
       score -= 1500 + j*m; //m or 7? 
      } 
      if (u == 1) 
      { 
       score -= 300; 
      } 
     } 
     if (thnk[x, y] == (int*)plyr && thnk[x, y - 1] == (int*)plyr) 
     { 
      u = 0; j = 0; 
      for (int z = y - 1; z > -1; z--) 
      { 
       if (thnk[x, z] == (int*)ai) 
       { 
        u = 1; 
       } 
       if (arry[x, z] == 0) 
       { 
        j++; 
       } 
      } 
      if (u == 0) 
      { 
       score -= 1500 + j*m; //m or 7? 
      } 
      if (u == 1) 
      { 
       score -= 300; 
      } 
     } 
     if (thnk[x, y] == (int*)plyr) 
     { 
      u = 0; 
      for (z = y - 1; y > -1; y--) 
      { 
       if (thnk[x, z] == (int*)ai) 
       { 
        u = 1; 
       } 
      } 
      if (u == 1) 
      { 
       score += 30; 
      } 
      if (thnk[x, y] == (int*)ai) 
      { 
       u = 0; 
       for (z = y - 1; z > -1; z--) 
       { 
        if (thnk[x, z] == (int*)plyr) 
        { 
         u = 1; 
        } 
       } 
       if (u == 1) 
       { 
        score -= 30; 
       } 
      } 
     } 
    } 
} 
return score; 
} 

//check score of current hole AI is contemplating 
int AIConnectBoard::checkhole(int x, int y) 
{ 
int score = 0; 
int max, min; 
int d0 = 0, d1 = 0, d2 = 0, d3 = 0; 

//d1 ai check rows 
if ((x + 1)<m && (y - 1)>-1) 
{ 
    if (arry[x + 1, y - 1] == (int*)ai) 
    { 
     d1++; 
     if ((x + 2)<m && (y - 2)>-1) 
     { 
      if (arry[x + 2, y - 2] == (int*)ai) 
      { 
       d1++; 
       if ((x + 3)<m && (y - 3)>-1) 
       { 
        if (arry[x + 3, y - 3] == (int*)ai) 
        { 
         d1++; 
        } 
       } 
      } 
     } 
    } 
} 

//d1 ai check columns 
if ((x - 1) > -1 && (y + 1) < n) 
{ 
    if (arry[x - 1, y + 1] == (int*)ai) 
    { 
     d1++; 
     if ((x - 2) >-1 && (y + 2) < n) 
     { 
      if (arry[x - 2, y + 2] == (int*)ai) 
      { 
       d1++; 
       if ((x - 3) >-1 && (y + 3) < n) 
       { 
        if (arry[x - 3, y + 3] == (int*)ai) 
        { 
         d1++; 
        } 
       } 
      } 
     } 
    } 
} 
//d2 ai check rows 
if ((x - 1)<m && (y - 1)>-1) 
{ 
    if (arry[x - 1, y - 1] == (int*)ai) 
    { 
     d2++; 
     if ((x - 2)<m && (y - 2)>-1) 
     { 
      if (arry[x - 2, y - 2] == (int*)ai) 
      { 
       d2++; 
       if ((x - 3)<m && (y - 3)>-1) 
       { 
        if (arry[x - 3, y - 3] == (int*)ai) 
        { 
         d2++; 
        } 
       } 
      } 
     } 
    } 
} 
//d2 ai check columns 
if ((x + 1) > -1 && (y + 1) < n) 
{ 
    if (arry[x + 1, y + 1] == (int*)ai) 
    { 
     d2++; 
     if ((x + 2) >-1 && (y + 2) < n) 
     { 
      if (arry[x + 2, y + 2] == (int*)ai) 
      { 
       d2++; 
       if ((x + 3) >-1 && (y + 3) < n) 
       { 
        if (arry[x + 3, y + 3] == (int*)ai) 
        { 
         d2++; 
        } 
       } 
      } 
     } 
    } 
} 
//d0 ai check 
if ((y - 1) > -1) 
{ 
    if (arry[x, y - 1] == (int*)ai) 
    { 
     d0++; 
     if ((y - 2) > -1) 
     { 
      if (arry[x, y - 2] == (int*)ai) 
      { 
       d0++; 
       if ((y - 3) > -1) 
       { 
        if (arry[x, y - 3] == (int*)ai) 
        { 
         d0++; 
        } 
       } 
      } 
     } 
    } 
} 
//d3 ai check 
if ((x - 1) > -1) 
{ 
    if (arry[x - 1, y] == (int*)ai) 
    { 
     d3++; 
     if ((x - 2) > -1) 
     { 
      if (arry[x - 2, y] == (int*)ai) 
      { 
       d3++; 
       if ((x - 3) > -1) 
       { 
        if (arry[x - 3, y] == (int*)ai) 
        { 
         d3++; 
        } 
       } 
      } 
     } 
    } 
} 
//d3 ai check rows 
if (x + 1 < m) 
{ 
    if (arry[x + 1, y] == (int*)ai) 
    { 
     d3++; 
     if (x + 2 < m) 
     { 
      if (arry[x + 2, y] == (int*)ai) 
      { 
       d3++; 
       if (x + 3 < m) 
       { 
        if (arry[x + 3, y] == (int*)ai) 
        { 
         d3++; 
        } 
       } 
      } 
     } 
    } 
} 
max = d0; 
//score manipulations 
if (d1 > max){ max = d1; } 
if (d2 > max){ max = d2; } 
if (d3 > max){ max = d3; } 
if (max == 2) 
{ 
    score += 5; 
} 
if (max > 2) 
{ 
    score += 71; 
    thnk[x, y] = (int*)ai; 
    if ((d1 < 3) && (d2 < 3) && (d3 < 3)) 
    { 
     score -= 10; 
    } 
} 
//d1 plyr check rows 
if ((x + 1)<m && (y - 1)>-1) 
{ 
    if (arry[x + 1, y - 1] == (int*)plyr) 
    { 
     d1++; 
     if ((x + 2)<m && (y - 2)>-1) 
     { 
      if (arry[x + 2, y - 2] == (int*)plyr) 
      { 
       d1++; 
       if ((x + 3)<m && (y - 3)>-1) 
       { 
        if (arry[x + 3, y - 3] == (int*)plyr) 
        { 
         d1++; 
        } 
       } 
      } 
     } 
     } 
} 
//d1 plyr check columns 
if ((x - 1) > -1 && (y + 1) < n) 
{ 
    if (arry[x - 1, y + 1] == (int*)plyr) 
    { 
     d1++; 
     if ((x - 2) >-1 && (y + 2) < n) 
     { 
      if (arry[x - 2, y + 2] == (int*)plyr) 
      { 
       d1++; 
       if ((x - 3) >-1 && (y + 3) < n) 
       { 
        if (arry[x - 3, y + 3] == (int*)plyr) 
        { 
         d1++; 
        } 
       } 
      } 
     } 
    } 
} 
//d2 plyr check rows 
if ((x - 1)<m && (y - 1)>-1) 
{ 
    if (arry[x - 1, y - 1] == (int*)plyr) 
    { 
     d2++; 
     if ((x - 2)<m && (y - 2)>-1) 
     { 
      if (arry[x - 2, y - 2] == (int*)plyr) 
      { 
       d2++; 
       if ((x - 3)<m && (y - 3)>-1) 
       { 
        if (arry[x - 3, y - 3] == (int*)plyr) 
        { 
         d2++; 
        } 
       } 
      } 
     } 
    } 
} 
//d2 plyr check columns 
if ((x + 1) > -1 && (y + 1) < n) 
{ 
    if (arry[x + 1, y + 1] == (int*)plyr) 
    { 
     d2++; 
     if ((x + 2) >-1 && (y + 2) < n) 
     { 
      if (arry[x + 2, y + 2] == (int*)plyr) 
      { 
       d2++; 
       if ((x + 3) >-1 && (y + 3) < n) 
       { 
        if (arry[x + 3, y + 3] == (int*)plyr) 
        { 
         d2++; 
        } 
       } 
      } 
     } 
    } 
} 
//d0 plyr check 
if ((y - 1) > -1) 
{ 
    if (arry[x, y - 1] == (int*)plyr) 
    { 
     d0++; 
     if ((y - 2) > -1) 
     { 
      if (arry[x, y - 2] == (int*)plyr) 
      { 
       d0++; 
       if ((y - 3) > -1) 
       { 
        if (arry[x, y - 3] == (int*)plyr) 
        { 
         d0++; 
        } 
       } 
      } 
     } 
    } 
} 
//d3 plyr check 
if ((x - 1) > -1) 
{ 
    if (arry[x - 1, y] == (int*)plyr) 
    { 
     d3++; 
     if ((x - 2) > -1) 
     { 
      if (arry[x - 2, y] == (int*)plyr) 
      { 
       d3++; 
       if ((x - 3) > -1) 
       { 
        if (arry[x - 3, y] == (int*)plyr) 
        { 
         d3++; 
        } 
       } 
      } 
     } 
    } 
} 
//d3 ai check rows 
if (x + 1 < m) 
{ 
    if (arry[x + 1, y] == (int*)plyr) 
    { 
     d3++; 
     if (x + 2 < m) 
     { 
      if (arry[x + 2, y] == (int*)plyr) 
      { 
       d3++; 
       if (x + 3 < m) 
       { 
        if (arry[x + 3, y] == (int*)plyr) 
        { 
         d3++; 
        } 
       } 
      } 
     } 
    } 
} 
min = d0; 
//score manipulations 
if (d1 > min){ min = d1; } 
if (d2 > min){ min = d2; } 
if (d3 > min){ min = d3; } 
if (min == 2) 
{ 
    score -= 4; 
} 
if (min > 2) 
{ 
    score -= 70; 
    thnk[x, y] = (int*)plyr; 
    if ((d1 < 3) && (d2 < 3) && (d3 < 3)) 
    { 
     score += 10; 
    } 
} 
return score; 

} 

//check for win condition (i.e. "conNum" in a row) 
int AIConnectBoard::checkwin() 
{ 
int r = 0; 
int x, y; 
for (y = 2; y > -1; y--) 
{ 
    for (x = 0; x < m; x++) 
    { 
     checku(x, y, r); 
    } 
} 
for (y = 0; y < n; y++) 
{ 
    for (x = 0; x < conNum; x++) 
    { 
     check2r(x, y, r); 
    } 
} 
for (y = 2; y>-1; y--) 
{ 
    for (x = 0; x < conNum; x++) 
    { 
     checkr(x, y, r); 
    } 
} 
for (y = 2; y>-1; y--) 
{ 
    for (x = 3; x < m; x++) 
    { 
     checkl(x, y, r); 
    } 
} 
return r; 
} 


//check win functions 
    void AIConnectBoard::checku(int x, int y, int& r) 
{ 
if ((arry[x, y] == (int*)2) && (arry[x, y + 1] == (int*)2) && (arry[x, y + 2] == (int*)2) && (arry[x, y + 3] == (int*)2))r = 2; 
if ((arry[x, y] == (int*)1) && (arry[x, y + 1] == (int*)1) && (arry[x, y + 2] == (int*)1) && (arry[x, y + 3] == (int*)1))r = 1; 
} 

void AIConnectBoard::check2r(int x, int y, int& r) 
{ 
if ((arry[x, y] == (int*)2) && (arry[x + 1, y] == (int*)2) && (arry[x + 2, y] == (int*)2) && (arry[x + 3, y] == (int*)2))r = 2; 
if ((arry[x, y] == (int*)1) && (arry[x + 1, y] == (int*)1) && (arry[x + 2, y] == (int*)1) && (arry[x + 3, y] == (int*)1))r = 1; 
} 

void AIConnectBoard::checkr(int x, int y, int& r) 
{ 
if ((arry[x, y] == (int*)2) && (arry[x + 1, y + 1] == (int*)2) && (arry[x + 2, y + 2] == (int*)2) && (arry[x + 3, y + 3] == (int*)2))r = 2; 
if ((arry[x, y] == (int*)1) && (arry[x + 1, y + 1] == (int*)1) && (arry[x + 2, y + 2] == (int*)1) && (arry[x + 3, y + 3] == (int*)1))r = 1; 
} 

void AIConnectBoard::checkl(int x, int y, int& r) 
{ 
if ((arry[x, y] == (int*)2) && (arry[x - 1, y + 1] == (int*)2) && (arry[x - 2, y + 2] == (int*)2) && (arry[x - 3, y + 3] == (int*)2))r = 2; 
if ((arry[x, y] == (int*)1) && (arry[x - 1, y + 1] == (int*)1) && (arry[x - 2, y + 2] == (int*)1) && (arry[x - 3, y + 3] == (int*)1))r = 1; 
} 
+0

使用'new'會引起麻煩,特別是當你沒有清理任何東西時。爲什麼'b'是一個指針呢?至於二維數組,你可以使用矢量矢量,但最好找到一個矩陣類來避免鋸齒。 – chris

+0

This:'int ** thnk = new int * [m,n];'不是C++ ..更多C#。那麼..它將在C++中編譯,但它不會做你認爲它的工作。 – Brandon

回答

0

這裏有一些問題正在進行。首先:關於在C++中使用「new」運算符的一些信息:

C++中的新增功能與Java(或其他類似語言)中的新功能不同。它的功能基本相同,但沒有垃圾收集器來清理你的垃圾。新增局部變量,然後不做任何事情(並且不會在函數結束之前刪除它們,例如在「set board」函數中!)會導致稱爲「內存泄漏」的問題。

換句話說,對於你的程序的生命,有一塊內存分配給這個特定的數據,永遠不會返回到你的程序分配的分配空間,直到你釋放它與根據實現的確切性質,適當的刪除(或者在數組的情況下,delet []),釋放或自由方法/函數。所以,這就是說:按值初始化事物,靜態地執行它(如不使用「新」),並確保使用正確的變量,而不是聲明新的不會去的新變量任何地方。

多數民衆贊成瞪着我的臉現在第一件事情是這樣的:

int* top = new int[n]; 

在組板功能找到。這是動態分配一個新的大小爲「n」的整數數組。

這樣做的問題是它是一個局部變量,並且從不刪除,因此當您離開此作用域時(即函數返回時),指向此數據的指針會丟失,這意味着您永遠無法釋放直到你的程序結束(假設操作系統可以處理,大多數情況下可以,但依賴於它仍然不是很好的做法),並且實際的「頂部」數組永遠不會真正初始化,因此「訪問錯誤」。

+0

不幸的是,它必須是一個動態數組,因爲在用戶給出尺寸之前不會設置電路板。我將嘗試下面提到的std :: vector修復,看看是否開始清理所有新的(我只用了,因爲我不能找出任何其他方式的動態數組,我檢查的每個資源都顯示它使用新的命令)。 – zearth

+0

使用任何一個矢量(我會建議)或數組類(它允許固定的大小,雖然你可以很容易地讓你的矢量在到達用戶輸入時停止添加空格)會使它可行。還有一種選擇是使用智能指針,它的行爲或多或少像普通指針,但當性能不成問題時,它比使用「新」更「C++ ish」。 [std :: array](http://en.cppreference.com/w/cpp/container/array) [std :: vector](http://www.cplusplus.com/reference/vector/矢量/) [智能指針](http://msdn.microsoft.com/en-us/library/hh279674.aspx) – Gurgadurgen

0

我沒有調試你的代碼呢。但要做的第一件事就是從代碼中取出所有新的和刪除的內容,並使用std::vector代替。除了簡化代碼之外,這將消除內存分配錯誤作爲問題的來源。

如果你這樣做,仍然有問題,然後在故障行上,將arry[c][top[c]]更改爲array.at(c).at(top[c])並編寫一個異常處理程序。如果你得到一個異常拋出,那麼這意味着你試圖訪問出界,這是比段錯誤更可靠的信息。

現在,你有很多代碼,如thnk[x, y] == (int*)aiai是一個整數,所以你認爲(int *)ai會給你帶來什麼?

看起來好像你沒有轉換就得到了這一行的編譯器警告,所以你猜對了一些代碼來使警告消失。這在C++中是一個非常糟糕的主意。除非你100%確定你在做什麼,否則不要使用演員。

該警告是因爲thnk[x, y]int *,即int的地址。你可能意思是thnk[x][y]。在C中,表達式x, y的意思是「評估x,忽略結果,並使用y」。所以它相當於thnk[y]

+0

我使用的演員,因爲我知道該數組是作爲一個int *和演員「固定「它(擺脫了錯誤)。不幸的是,我正在處理一些我首先做的c#代碼,並試圖翻譯它(這些動態數組在C#中工作得非常好,與我注意到的不同)。我確實嘗試了[x] [y] == int,但遇到了一些問題,但生病嘗試向量以查看是否能解決問題 – zearth

+0

該數組不是'int *',這就是整個問題。 –