有關於〝位置存取錯誤的問題〞 |
答題得分者是:harpist
|
zeckman
一般會員 發表:1 回覆:4 積分:1 註冊:2007-06-26 發送簡訊給我 |
各位大大,晚安。
末學在此想求益一個問題, 這是我期末報告, 做的是直接在StringGrid上填入資料, 然後計算出結果的程式, 其中, 用了兩個ScrollBar(n*m)來控制StringGrid的大小, 程式中有使用動態配置記憶體, 來配置二維陣列。 在執行程式時, 若n=m時, 就沒有問題的正常產生所要的結果, 可是當n不等於m時, 就會出現如圖的訊息。 在Delphi中搜到一篇文章, 也提及此問題的答覆, 說是有關給定的陣列空間不足所造成的問題, 可是我給的是剛好的, 在許多嘗試後, 還是發生相同的問題, 同學說可能是只能產生方陣而已, 但網路與書上都未提及只能產生方陣的限制, 目前實在想不到其他嘗試的方式了, 想說請益ㄧ下各位前輩, 是否能指點一下迷津呢?? 以下是我的原始程式碼。 //--------------------------------------------------------------------------- #include #pragma hdrstop #include #include #include "Math.hpp" #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::ScrollBar1Change(TObject *Sender) { m=ScrollBar1->Position; Edit6->Text=IntToStr(m); } //--------------------------------------------------------------------------- void __fastcall TForm1::ScrollBar2Change(TObject *Sender) { n=ScrollBar2->Position; Edit7->Text=IntToStr(n); } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1Click(TObject *Sender) { StringGrid1->EditorMode = true; //先定義表格欄與列的長度( 2是包含顯示p(x)以及p(y)) StringGrid1->ColCount=m 2; StringGrid1->RowCount=n 2; //在StringGrid的(0,0)位置顯示列表 StringGrid1->Cells[0][0]="p(x,y)"; //在StringGrid的第1欄與列顯示數字,好分辨座標 StringGrid1->Cells[0][1]="p(x=0)"; for(int i=2; i<=n; i ) StringGrid1->Cells[0][i]=i-1; StringGrid1->Cells[0][n 1]="p(y)"; //StringGrid1->Cells[0][3]="p(x=2)"; //StringGrid1->Cells[0][4]="p(x=3)"; StringGrid1->Cells[1][0]="p(y=0)"; for(int j=2; j<=m; j ) StringGrid1->Cells[j][0]=j-1; StringGrid1->Cells[m 1][0]="p(x)"; //StringGrid1->Cells[3][0]="p(y=2)"; //StringGrid1->Cells[4][0]="p(y=3)"; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { float H_x,H_y,H,I,H_ry,D; float **p1=new float*[n]; for(int a=1; a<=n; a ) p1[a]=new float[m]; float **p2=new float*[n]; for(int b=1; b<=n; b ) p2[b]=new float[m]; float **p3=new float*[n]; for(int c=1; c<=n; c ) p3[c]=new float[m]; float **h=new float*[n]; for(int d=1; d<=n; d ) h[d]=new float[m]; float *p_x=new float[n]; float *p_y=new float[m]; float *h_x=new float[n]; float *h_y=new float[m]; float *h_h=new float[n]; float *hh=new float[m]; for(int i=1; i<=n; i ) for(int j=1; j<=m; j ) { p1[j][i]=StrToFloat(StringGrid1->Cells[j][i]); p2[j][i]=StrToFloat(StringGrid1->Cells[j][i]); p3[j][i]=StrToFloat(StringGrid1->Cells[j][i]); //Edit1->Text=FloatToStr(p[3][1]); } ...... } //--------------------------------------------------------------------------- 麻煩了, 感恩。 |
harpist
資深會員 發表:3 回覆:251 積分:430 註冊:2002-10-03 發送簡訊給我 |
<textarea class="cpp" rows="10" cols="60" name="code">float **p1=new float*[n];
</textarea>
以上面p1 為例宣告了n個元素。 <textarea class="cpp" rows="10" cols="60" name="code">for(int i=1; i<=n; i ) for(int j=1; j<=m; j ) { p1[j][i]=StrToFloat(StringGrid1->Cells[j][i]); p2[j][i]=StrToFloat(StringGrid1->Cells[j][i]); p3[j][i]=StrToFloat(StringGrid1->Cells[j][i]); //Edit1->Text=FloatToStr(p[3][1]); } </textarea> p1陣列索引值是從0開始,所以p1的範圍是p1[0]~p1[n-1],而你卻要存取p1[1]~p1[m]當然不對。
------
~§~迷時師渡,悟了自渡~§~
編輯記錄
harpist 重新編輯於 2007-06-27 01:26:25, 註解 無‧
|
zeckman
一般會員 發表:1 回覆:4 積分:1 註冊:2007-06-26 發送簡訊給我 |
感恩harpist前輩的指點,
但嘗試著更改後, 同樣的問題還是存在, 只要當n不等於m時, 還是出現了錯誤。 末學不才, 想再請問一下前輩, 還有其他可能因素嗎?? 感恩萬分。 以下為修改後的程式碼: float **p1=new float*[n]; for(int a=0; a float **p2=new float*[n]; for(int b=0; b float **p3=new float*[n]; for(int c=0; c float **h=new float*[n]; for(int d=0; d for(int i=0; i p1[j][i]=StrToFloat(StringGrid1->Cells[j 1][i 1]); p2[j][i]=StrToFloat(StringGrid1->Cells[j 1][i 1]); p3[j][i]=StrToFloat(StringGrid1->Cells[j 1][i 1]); } |
harpist
資深會員 發表:3 回覆:251 積分:430 註冊:2002-10-03 發送簡訊給我 |
<textarea class="cpp" rows="10" cols="60" name="code">//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
float H_x,H_y,H,I,H_ry,D;
float **p1=new float*[m];
for(int a=0;a
另外請參考如何編輯程式碼
------
~§~迷時師渡,悟了自渡~§~ |
zeckman
一般會員 發表:1 回覆:4 積分:1 註冊:2007-06-26 發送簡訊給我 |
<textarea class="cpp" rows="10" cols="60" name="code">void __fastcall TForm1::Button1Click(TObject *Sender)
{
float **p1=new float*[n];
for(int a=0; a
感恩前輩的提醒與協助, 這樣的確好看多了^^ 誠如前輩所PO的部份, 是我修正後的程式, 但是主要原因還是沒有去除, 當n不等於m時, 就會出現同樣的錯誤訊息。 懇請前輩再指教, 無限感恩。 |
harpist
資深會員 發表:3 回覆:251 積分:430 註冊:2002-10-03 發送簡訊給我 |
<textarea class="cpp" rows="10" cols="60" name="code">
int m = StringGrid1->ColCount-2;
int n = StringGrid1->RowCount-2;
float **p1=new float*[m];
for(int a=0;a
------
~§~迷時師渡,悟了自渡~§~ |
zeckman
一般會員 發表:1 回覆:4 積分:1 註冊:2007-06-26 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |