線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3487
推到 Plurk!
推到 Facebook!

AnsiString 不能做字串相加嗎?

尚未結案
WHungYun
一般會員


發表:5
回覆:6
積分:2
註冊:2004-04-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-04-20 21:18:44 IP:61.221.xxx.xxx 未訂閱
一個簡單的程式如下:    AnsiString astring; astring = "aaa" + "bbb";    編譯後會出現 Invalid pointer addition 請問是?? 不好意思感覺問的問題都沒啥深度...
pwipwi
版主


發表:68
回覆:629
積分:349
註冊:2004-04-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-04-20 21:38:46 IP:211.76.xxx.xxx 未訂閱
引言: 一個簡單的程式如下: AnsiString astring; astring = "aaa" "bbb"; 編譯後會出現 Invalid pointer addition 請問是?? 不好意思感覺問的問題都沒啥深度... ----------------- 新手上路,請多包涵 ------------------ < face="Verdana, Arial, Helvetica"> 試試
AnsiString astring;
astring = "aaa"
astring  = "bbb";     
anpino
版主


發表:31
回覆:477
積分:231
註冊:2003-01-02

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-04-21 09:33:11 IP:218.32.xxx.xxx 未訂閱
引言: 一個簡單的程式如下: AnsiString astring; astring = "aaa" "bbb"; 編譯後會出現 Invalid pointer addition >< face="Verdana, Arial, Helvetica"> 要用你的方式對AnsiString操作是不可能的。 因為AnsiString是繼承C 標準中的String物件, 而String已經講明了字串累加時,2個累加字串中一定要有一個String物件。 也就是說,你只能這樣做: astring = "aaa"; astring = "aaa" String("bbb"); astring = String("aaa") "bbb"; astring = "aaa" String("bbb") "ccc"; ... 以此類推。 請先參考C 標準文件。 ------------------------------- 數學系是內功很強(邏輯/分析) 資工系是招式很多(程式技巧) 就像令狐沖
folkchen
高階會員


發表:9
回覆:232
積分:173
註冊:2003-10-09

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-04-21 09:46:24 IP:211.20.xxx.xxx 未訂閱
你試試    AnsiString astring; astring = (AnsiString)"aaa" "bbb";
jimmy_and_you
初階會員


發表:20
回覆:74
積分:33
註冊:2003-05-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-04-21 12:09:56 IP:203.70.xxx.xxx 未訂閱
引言:
引言: 一個簡單的程式如下: AnsiString astring; astring = "aaa" "bbb"; 編譯後會出現 Invalid pointer addition 請問是?? 不好意思感覺問的問題都沒啥深度... ----------------- 新手上路,請多包涵 ------------------ < face="Verdana, Arial, Helvetica"> 試試
AnsiString astring;
astring = "aaa"
astring  = "bbb";     
好奇的問一下 AnsiString 可以用 = 的運算嗎? astring = "aaa" astring = "bbb"; 好像答案會變成 "bbb" 而不是"aaabbb"
Albertz
初階會員


發表:2
回覆:57
積分:31
註冊:2002-09-05

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-04-21 12:36:05 IP:211.20.xxx.xxx 未訂閱
The following information is from Borland TeamB    Don't use the = AnsiString operator on properties Examine the following code that attempts to add some exclamation points to the string that is already in a label control: Label1->Caption = "!!!!!!!"; When you use the = operator on the Caption property, the compiler creates code that constructs a new temporary AnsiString object. The compiler then calls the GetText function to copy the contents of the label into the temporary variable. Next, the compiler constructs another AnsiString object and initializes it with the string "!!!!!". Finally, the compiler calls the = operator of the first temporary object to combine the two strings. The problem is that the compiler does not generator code to write the resulting temporary AnsiString value back into the Caption property. Instead, the temporary object is deleted because it is no longer needed. The assembly code looks something like this: // assembly output for the statement Label1->Caption = "!!!!!!!"; mov ... lea ... call System::AnsiString::AnsiString() // create temp object mov ... inc ... mov ... mov ... call Controls::TControl::GetText() // read Caption into temp object lea ... push ... mov ... lea ... call System::AnsiString::AnsiString(char *) // create AnsiString for "!!!!" inc ... lea ... pop ... call System::AnsiString::operator = // combine the two strings dec ... lea ... mov ... call System::AnsiString::~AnsiString() // destroy one temp dec ... lea ... mov ... call System::AnsiString::~AnsiString() // destroy the other temp In the assembly code above, locate the = operator call. Notice that nothing is done with the resulting string after the = operator returns. After the = operator returns, the strings are destroyed. In order for the property assignment to take affect, the resulting string should be passed to the SetText function. Because the write method for the property is not called, the TLabel object is not modified. Through experimentation, I have found that the = operator does work on integer properties, such as the Width property of the form. However, in order to avoid confusion, it may be wise to avoid the = operator on all properties. The same can be said for the other combination operators (-= , *= , /=, ^= , and so on).
WHungYun
一般會員


發表:5
回覆:6
積分:2
註冊:2004-04-19

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-04-21 12:46:24 IP:61.221.xxx.xxx 未訂閱
瞭解...謝謝回答...    但有一疑惑是...廠商所提供的程式http://home.pchome.com.tw/hot/whungyun/BCB4-DEMO.ZIP 裡面有一段程式碼如下    AnsiString aTransferDataR;    aTransferDataR="SPX="+Edit4_SP->Text+";"+                   "ACX="+Edit5_AC->Text+";"+                   "DCX="+Edit6_DC->Text+";"+                   "PRX=-"+Edit3_PR->Text+";"+"BGX" ; 編譯是可行的    但我自己在開一新專案同樣程式碼編譯,卻不行 編譯後會出現 Invalid pointer addition    請問這也是同原因嗎?如是,為何廠商的程式可以編譯沒問題 再次謝謝各位!..
Albertz
初階會員


發表:2
回覆:57
積分:31
註冊:2002-09-05

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-04-21 13:12:30 IP:211.20.xxx.xxx 未訂閱
引言: 但有一疑惑是...廠商所提供的程式http://home.pchome.com.tw/hot/whungyun/BCB4-DEMO.ZIP 裡面有一段程式碼如下 AnsiString aTransferDataR; aTransferDataR="SPX=" Edit4_SP->Text ";" "ACX=" Edit5_AC->Text ";" "DCX=" Edit6_DC->Text ";" "PRX=-" Edit3_PR->Text ";" "BGX" ; 編譯是可行的 但我自己在開一新專案同樣程式碼編譯,卻不行 編譯後會出現 Invalid pointer addition
這段 code 只是做單純的字串相加, 並沒有用到 " =" 運算子. 建議你再看清楚一點.
folkchen
高階會員


發表:9
回覆:232
積分:173
註冊:2003-10-09

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-04-21 13:20:13 IP:211.20.xxx.xxx 未訂閱
引言:
引言:
引言: 一個簡單的程式如下: AnsiString astring; astring = "aaa" "bbb"; 編譯後會出現 Invalid pointer addition 請問是?? 不好意思感覺問的問題都沒啥深度... ----------------- 新手上路,請多包涵 ------------------ < face="Verdana, Arial, Helvetica"> 試試
AnsiString astring;
astring = "aaa"
astring  = "bbb";     
好奇的問一下 AnsiString 可以用 = 的運算嗎? astring = "aaa" astring = "bbb"; 好像答案會變成 "bbb" 而不是"aaabbb"
AnsiString astring; astring = (AnsiString)"aaa" "bbb"; astring = "ccc"; 2種寫法都可以用,因為我的程式都有大量用到這2種寫法在串SQL字串
WHungYun
一般會員


發表:5
回覆:6
積分:2
註冊:2004-04-19

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-04-21 13:28:34 IP:61.221.xxx.xxx 未訂閱
To Albertz     不好意思我最初的問題不是問"+=" 運算子 是其他版友回答說AnsiString 字串相加可用"+=" 運算子    我的問題是AnsiString直接做字串相加會出錯,如下     
AnsiString aTransferDataR;
aTransferDataR="SPX=" Edit4_SP->Text ";" 
"ACX=" Edit5_AC->Text ";" 
"DCX=" Edit6_DC->Text ";" 
"PRX=-" Edit3_PR->Text ";" "BGX" ; 
編譯後會出現 Invalid pointer addition 在anpino大大有回應提到這問題是 因為AnsiString本來就不支援這語法 但相同程式碼,廠商給我的程式卻是可以編譯沒問題 再次感謝你!...^_^ 發表人 - WHungYun 於 2004/04/21 13:38:52
Albertz
初階會員


發表:2
回覆:57
積分:31
註冊:2002-09-05

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-04-21 14:11:20 IP:211.20.xxx.xxx 未訂閱
引言: AnsiString astring; astring = (AnsiString)"aaa" "bbb"; astring = "ccc"; 2種寫法都可以用,因為我的程式都有大量用到這2種寫法在串SQL字串
astring = (AnsiString)"aaa" "bbb" 這種寫法當然可以, 因為"aaa"這個字元陣列已經自動轉成 AnsiString. 而 AnsiString 有提供 " " 運算子. astring = "ccc"; 至於上面這種寫法, 以C 的觀念,它是對的.但是你確定每個 object 都 可以動作正常嗎?TeamB 列出了幾個在 C Builder 常犯的錯誤.有興趣 可以看一看.犯不著跟 compiler 硬碰硬吧! The TeamB guide to avoiding common mistakes in C Builder. http://www.bcbdev.com/articles/suggest.htm# 請參考 section 1.3 發表人 - Albertz 於 2004/04/21 14:14:57
jimmy_and_you
初階會員


發表:20
回覆:74
積分:33
註冊:2003-05-12

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-04-21 14:19:31 IP:203.70.xxx.xxx 未訂閱
引言:
引言:
引言:
引言: 一個簡單的程式如下: AnsiString astring; astring = "aaa" "bbb"; 編譯後會出現 Invalid pointer addition 請問是?? 不好意思感覺問的問題都沒啥深度... ----------------- 新手上路,請多包涵 ------------------ < face="Verdana, Arial, Helvetica"> 試試
AnsiString astring;
astring = "aaa"
astring  = "bbb";     
好奇的問一下 AnsiString 可以用 = 的運算嗎? astring = "aaa" astring = "bbb"; 好像答案會變成 "bbb" 而不是"aaabbb"
AnsiString astring; astring = (AnsiString)"aaa" "bbb"; astring = "ccc"; 2種寫法都可以用,因為我的程式都有大量用到這2種寫法在串SQL字串
剛剛又試了一下...發現自己定義的AnsiString 可以做 = 的運算 但元件的屬性卻不行 例如: Edit1->Text = "123"; Edit1->Text = "456"; 答案卻是"123" 可見的Albertz所提的說明似乎也沒錯....所以保險的方法還是不要用 = 的語法為上策
anpino
版主


發表:31
回覆:477
積分:231
註冊:2003-01-02

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-04-21 15:36:58 IP:218.32.xxx.xxx 未訂閱
引言: 我的問題是AnsiString直接做字串相加會出錯,如下
AnsiString aTransferDataR;
aTransferDataR="SPX=" Edit4_SP->Text ";" 
"ACX=" Edit5_AC->Text ";" 
"DCX=" Edit6_DC->Text ";" 
"PRX=-" Edit3_PR->Text ";" "BGX" ; 
編譯後會出現 Invalid pointer addition 在anpino大大有回應提到這問題是 因為AnsiString本來就不支援這語法
WHungYun您好, 請注意紅色字體部分:
AnsiString aTransferDataR;
aTransferDataR="SPX=" Edit4_SP->Text ";" 
"ACX=" Edit5_AC->Text ";" 
"DCX=" Edit6_DC->Text ";" 
"PRX=-" Edit3_PR->Text ";" "BGX" ; 
這些都是String型態, 當然沒有問題。 只要累加字串中,最前面2個字串有一個是String型態就可以, aTransferDataR最前面2個字串有一個是Edit4_SP->Text(String型態)。 ------------------------------- 數學系是內功很強(邏輯/分析) 資工系是招式很多(程式技巧) 就像令狐沖
WHungYun
一般會員


發表:5
回覆:6
積分:2
註冊:2004-04-19

發送簡訊給我
#14 引用回覆 回覆 發表時間:2004-04-22 20:13:48 IP:61.221.xxx.xxx 未訂閱
感謝anpino 我又學到一課 也感謝其他各位,讓我學到很多....
系統時間:2024-07-04 12:08:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!