[問題] 請教關於陣列

看板C_and_CPP (C/C++)作者 (梅森)時間16年前 (2009/03/17 12:00), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
#include "stdafx.h" #include "iostream" using namespace std; int main(void) { int p=1; cout<<"How many numbers?"<<endl; cin>>p; int A[p]; for(int i=0;i<p; i++) { cin>>A[i]; } cout<<"Before sort:"<<endl; for(int i=0;i<p; i++) { cout<<A[i]<<endl; } return 0; } 我卻得到錯誤訊息: .\作業1.2.cpp(9) : error C2057: 必須是常數運算式 .\作業1.2.cpp(9) : error C2466: 無法配置常數大小為 0 的陣列 ,常數必須為大於 0 的整數 .\作業1.2.cpp(9) : error C2133: 'A' : 未知的大小 請問問題出在哪裡呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.235

03/17 12:02, , 1F
int A[p];
03/17 12:02, 1F

03/17 12:03, , 2F
改成const int p吧
03/17 12:03, 2F

03/17 12:04, , 3F
不過你p要用輸入的得到 那只好用動態宣告了
03/17 12:04, 3F

03/17 12:24, , 4F
c++就用vector呀
03/17 12:24, 4F
※ 編輯: Mason0218 來自: 140.112.4.234 (03/17 15:32)

03/17 15:33, , 5F
瞭解了,謝謝
03/17 15:33, 5F
文章代碼(AID): #19lo1oDX (C_and_CPP)
文章代碼(AID): #19lo1oDX (C_and_CPP)