[問題] 請問struct裡的element可再動態宣告嗎?

看板C_and_CPP (C/C++)作者 (daSd)時間15年前 (2010/11/26 10:26), 編輯推噓2(206)
留言8則, 2人參與, 最新討論串1/1
請問struct裡的element可以再動態宣告嗎? head檔=== class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); struct rainfall{ float *value; }; rainfall *station; }; cpp檔=== void __fastcall TForm1::Button1Click(TObject *Sender) { int num=100; station = new rainfall[num]; //動態宣告station沒問題 for(int i=0; i<num; i++){ float *station[i].value = new float[num]; //上面這不行 //[C++ Error] Unit1.cpp(27): E2313 Constant expression required //[C++ Error] Unit1.cpp(27): E2141 Declaration syntax error } } 請問有類似這樣的寫法嗎? 新手笨問題請多包涵 謝謝 ※ 編輯: clough 來自: 140.115.61.78 (11/26 10:30)

11/26 10:41, , 1F
station[i]->value = new float[num];
11/26 10:41, 1F

11/26 10:41, , 2F
單純語法錯誤吧
11/26 10:41, 2F

11/26 10:42, , 3F
宣告和賦值不要混為一談
11/26 10:42, 3F

11/26 10:43, , 4F
啊, 應該是 station[i].value ...
11/26 10:43, 4F

11/26 10:59, , 5F
我把指標*拿掉用float station[i].value = new float[num];
11/26 10:59, 5F

11/26 10:59, , 6F
compile時還是一樣的錯誤.再麻煩了
11/26 10:59, 6F

11/26 11:06, , 7F
你為什麼不能把 float 拿掉呢? QQ
11/26 11:06, 7F

11/26 11:09, , 8F
抱歉..拿掉後執行無誤了..謝謝你
11/26 11:09, 8F
文章代碼(AID): #1CxnhBqg (C_and_CPP)
文章代碼(AID): #1CxnhBqg (C_and_CPP)