看板 C_and_CPP 關於我們 聯絡資訊
遇到的問題 我寫了一個 Class C, 裡面定義了一個 struct S, Class 有個函數 F(), 呼叫 F() 時會進行 malloc 動作 但 Complier 卻失敗了 請問我觀念是否有問題? 程式跑出來的錯誤結果: error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct S*' (or there is no acceptable conversion) Error executing cl.exe. 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) VC. 有問題的code: (請善用置底文標色功能) typedef struct _Data{ double *property; double cluster_d; // 與目前 cluster 距離 int cluster; // 所屬 cluster char *comment; // 註解 } *pData,Data; class EdisonKMeans{ private: unsigned data_cnt; unsigned K; unsigned Dim; Data data; public: void InitializeData(); .... }; void EdisonKMeans::InitializeData() { data = (Data*)malloc(sizeof(Data)*data_cnt); } 補充說明: -- 我期待 我等待 肩狹骨上的翅膀早些長出來 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 180.177.77.80 ※ 編輯: tropical72 來自: 180.177.77.80 (05/02 06:58)
fragileness:等號左邊型態是Data 右邊是Data* 當然不會讓你過了 05/02 07:04
tropical72:那請問我該如何改成我想要的? 05/02 07:10
tropical72:我直接用 sub function 呼叫是正常的. 但在 class 裡 05/02 07:11
tropical72:卻不行,有些疑惑 .. 05/02 07:11
fragileness:你用subfunction呼叫時怎麼寫的 我不太相信完全一樣 05/02 07:14
tropical72:Data* Malloc(unsigned cnt){ 05/02 07:17
tropical72:Data* a = (Data*)malloc(sizeof(Data)*cnt); 05/02 07:18
tropical72:for(unsigned i=0; i<cnt; i++){ a[i].cluster=i;} 05/02 07:18
tropical72:return a; } 05/02 07:19
fragileness:你自己比對一下哪裡不一樣 就知道要改哪裡了吧 05/02 07:19
fragileness:subfunction裡面的a 跟class裡的data 哪裡不一樣 05/02 07:19
fragileness:就改哪裡 05/02 07:20
tropical72:void EdisonKMeans::InitializeData(){ 05/02 07:22
tropical72:Data *a = (Data*)malloc(sizeof(Data)*data_cnt); 05/02 07:22
tropical72:data = a;} // - fail still. 抱歉, 資質不好 05/02 07:23
tropical72:Complier 後仍有同樣的 error message. 05/02 07:24
fragileness:還是不一樣啊 你subfunction裡面有data = a這件事嗎 05/02 07:25
tropical72:嗯..我另發文好了, 抱歉表達不好. 05/02 07:26
tropical72:= = 抱歉, 我手殘又眼殘了.. class 中的 Data data 05/02 07:32
tropical72:宣告錯了, 應是 Data *data; 05/02 07:32
tropical72:不過這樣後, 二個方法都可以正常執行了. 05/02 07:32
tropical72:謝謝 fragileness的指教.. 05/02 07:33