[問題] struct function (已解決)

看板C_and_CPP作者 (攸藍)時間10年前 (2014/02/22 01:13), 編輯推噓2(207)
留言9則, 5人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) G++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) Eigen 問題(Question): 想要把input struct的function 放到header file去 可是分開就不能compile,想問要header file要怎麼編寫? 下面是可以執行的code,想把f函數移到header file中。 我GOOGLE了,說可以先用typedef之類的,可是我typedef從沒成功過... 程式碼(Code):(請善用置底文網頁, 記得排版) http://codepad.org/R0xt9tWL 補充說明(Supplement): 解決方式如下: 我把它拆成以下兩個file.. **************** struct_f.h **************** using namespace Eigen; MatrixXd f(uLSIF_para& uLSIF_parameters) { return uLSIF_parameters.width_candidates; } ********************************************** **************** test.cpp **************** #include "Eigen/Dense" #include "Eigen/Core" using namespace Eigen; typedef struct { MatrixXd width_candidates; size_t posclass; } uLSIF_para; #include <time.h> #include <iostream> #include "struct_f.h" using namespace std; int main() { srand((unsigned int)time(NULL)); MatrixXd width_candidates(1, 13); width_candidates << 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3; size_t posclass = 1; uLSIF_para uLSIF_parameters = { width_candidates, posclass }; cout << f(uLSIF_parameters) << endl; return 0; } ******************************************* -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 36.238.86.146

02/22 01:19, , 1F
你可以試著說明一下你是怎麼分開的
02/22 01:19, 1F
已補上...

02/22 02:10, , 2F
解決方法沒有解決根本原因, compiler 已經告訴你
02/22 02:10, 2F

02/22 02:11, , 3F
你在 f 中使用 uLSIF_para 的定義可是你並沒有定義
02/22 02:11, 3F

02/22 02:12, , 4F
把 f 的實作 include uLSIF_para 的定義
02/22 02:12, 4F
我完全看不懂你在說什麼...怎麼辦...

02/22 02:22, , 5F
include struct_f.h前 typedef uLSIF_para不是嗎?
02/22 02:22, 5F

02/22 13:26, , 6F
基本上現在這樣有拆等於沒拆 @@
02/22 13:26, 6F
我只是想分成兩個檔案,一個檔案太長了... 不知道拆成兩個檔案還有什麼好處嗎?

02/22 14:40, , 7F
...只有我覺得不應該把沒有用到template的func impl放到
02/22 14:40, 7F

02/22 14:40, , 8F
header嗎...
02/22 14:40, 8F

02/22 15:04, , 9F
加個 inline 就沒有問題惹(本末倒置)
02/22 15:04, 9F
※ 編輯: celestialgod 來自: 36.238.86.146 (02/22 15:53)
文章代碼(AID): #1J1uawsM (C_and_CPP)