[問題] typedef struct 的問題?

看板C_and_CPP (C/C++)作者 (2+2=0)時間16年前 (2009/05/29 20:15), 編輯推噓3(304)
留言7則, 4人參與, 最新討論串1/1
剛有稍微爬一下文~不過不確定我想的對不對!! 以下是我的程式: #define MixtureNum 17 #define FeatDim 17 #define PatternNum 2 typedef struct{ double **GMMMean; double **GMMVar; }GMM; GMM *Model; Model=(GMM *)malloc(PatternNum*sizeof(GMM)); Model[index].GMMMean=(double **)malloc(MixtureNum*sizeof(double *)); Model[index].GMMVar=(double **)malloc(MixtureNum*sizeof(double *)); for(i=0;i<MixtureNum;i++){ Model[index].GMMMean[i]=(double *)malloc(FeatDim*sizeof(double)); Model[index].GMMVar[i]=(double *)malloc(FeatDim*sizeof(double)); } . . . 以下略過!! 這樣去compiler & linker 完全不會錯~那如果我今天多加了這些東西在上面: GMM *Model,*New_Model; (多加了 *New_Model 這項!! ) New_Model=(GMM *)malloc(1*sizeof(GMM)); New_Model.GMMMean=(double **)malloc(MixtureNum*sizeof(double *)); New_Model.GMMVar=(double **)malloc(MixtureNum*sizeof(double *)); for(i=0;i<MixtureNum;i++){ New_Model.GMMMean[i]=(double *)malloc(FeatDim*sizeof(double)); New_Model.GMMVar[i]=(double *)malloc(FeatDim*sizeof(double)); } 在這樣情況下 compiler 會出現4個錯誤 都是顯示 "error C2228: left of '.GMMMean' must have class/struct/union type" "error C2228: left of '.GMMVar' must have class/struct/union type" "error C2228: left of '.GMMMean' must have class/struct/union type" "error C2228: left of '.GMMVar' must have class/struct/union type" 會出現錯誤是因為 struct 在C++裡面的資料型態都為public的關係嗎? 因為它好像只認得Model裡面的東西 不認得New_Model裡面的東西!! 如果真是這樣 那如果我要宣告ㄧ個跟GMMㄧ模一樣的結構只能再重新定義囉!? ex: typedef struct{ double **BBB; double **CCC; }TTT; TTT *New_Model; . . . 這樣對嗎? 麻煩大家幫我解惑一下!! thanks!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.124.72.230

05/29 20:18, , 1F
改 New_Model->GMMMean,因為 New_Model 是 pointer
05/29 20:18, 1F

05/29 20:18, , 2F
建議你先看一下基本語法的書,不要亂猜錯誤原因
05/29 20:18, 2F

05/29 21:06, , 3F
我有點搞混了~在正常結構下~結構定義的變數要存取是透過"."
05/29 21:06, 3F

05/29 21:06, , 4F
來存取~而結構定義之指標才是透過"->"來間接存取不是嗎@@?
05/29 21:06, 4F

05/29 21:14, , 5F
不是
05/29 21:14, 5F

05/30 14:11, , 6F
New_Model是指標變數阿 所以要透過 -> 取得成員資料...
05/30 14:11, 6F

05/30 14:14, , 7F
不然你高興用(*New_Model).xxx或New_Model[0].xxx也可..
05/30 14:14, 7F
文章代碼(AID): #1A7z7gBP (C_and_CPP)
文章代碼(AID): #1A7z7gBP (C_and_CPP)