2015-01-04 131 views
0

我正在精神上,把事情搞砸到簡單,不管我怎麼樣或什麼嘗試,數組都不起作用?這是怎麼回事?'new'無法創建數組?

該代碼僅僅是來自cocos2dx的單個cpp helloworld。僅此而已,

double *Array = new double[333]; 
if (Array == nullptr) 
    CCLOG("Error: memory could not be allocated"); 

//initialize it 
for(int i = 0; i != 333; ++i){ 
    Array[i] = 333 - i; 

} 

for(int i = 0; i != 333; ++i){ 

    CCLOG("Hi %ld", Array[i]);  
} 

的循環總是打印0 ....

香港專業教育學院嘗試了很多圈,測試,該陣列是永遠陣列。它總是隻是一個int,或者double,或者我嘗試的任何類型的數組?

任何thoguhts?

VS2012 cocos2dx helloworld現在剝離的只是一個數組。 Windows 10

+3

你可能有'CCLOG'格式錯誤。 – juanchopanza 2015-01-04 10:24:25

+0

我同意@juanchopanza。不用'CCLOG',嘗試'cout << Array [i] <<「\ n」;'(或'printf(「%ld \ n」,Array [i]);'如果您願意的話。 – TonyK 2015-01-04 10:28:13

+1

%d是不整齊的整數。 – 2015-01-04 10:32:52

回答

6

因爲您試圖顯示一個格式爲%ld的雙精度格式。 %ld應該只用很長時間。恕我直言,你應該:

  • 要麼轉換Array[i]爲長:

    CCLOG("Hi %ld", (long) Array[i]); 
    
  • 或使用%g格式:

    CCLOG("Hi %g", Array[i]); 
    

這兩種方法都應該給你正確的顯示。

+0

格式化是我的錯誤,我沒有注意到在嘗試後,int,double,long,float等發生了改變。 問題..我的機器重新啓動後離開了? std :: srray人說? 這將如何動態使用? – 2015-01-04 21:03:47

0

%ld用於long intdouble使用%lf

0

格式是我的錯,我沒有注意到嘗試,詮釋後去改變它,雙擊,長,漂浮等

的problem..went了我的機器重新啓動後?

std :: srray? 這將如何動態使用/調整大小?