2013-01-11 74 views
0

我是C++的新手,我無法弄清楚爲什麼我的頭文件無法正常工作。如果我從主文件中包含功能fitzhough(),則一切正常。但是,如果我嘗試將其添加爲一個單獨的文件,它提供了錯誤:在C++中包含函數的麻煩

obj\Debug\main.o||In function `main':| 
C:\Users\Dan\Documents\C code\RK4\main.cpp|83|undefined reference to `fitzhough(double, Eigen::Matrix<double, 2, 1, 0, 2, 1>, double, double*)'| 
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===| 

我的代碼如下。任何幫助將不勝感激。

的main.cpp

# include <iostream> 
# include <fstream> 
# include <Eigen/Dense> 
# include "gnuplot.h" 
# include "addfitzhough.h" 

using namespace std; 

using namespace Eigen; 

Vector2d RK4(Vector2d (*f)(double, Vector2d, double, double*), double t, Vector2d z, double h, double u, double *Iion, int d) { 

    VectorXd Y1(d), Y2(d), Y3(d), Y4(d), Y1buf(d), Y2buf(d), Y3buf(d); 

    Y1 = z; 
    Y1buf = (*f)(t,Y1,u, Iion); 
    Y2 = z + 0.5*h*Y1buf; 
    Y2buf = (*f)(t+.5*h,Y2,u, Iion); 
    Y3 = z + 0.5*h*Y2buf; 
    Y3buf = (*f)(t+.5*h,Y3,u, Iion); 
    Y4 = z + h*Y3buf; 


    Vector2d yn = z + (h/6.0)*(Y1buf + 2.0*Y2buf + 2.0*Y3buf + (*f)(t+h,Y4,u, Iion)); 

    return yn; 
} 

int main() { 

    //int mydims = 2; 

    double u = 0; 
    double *Iion; 
    double h = .5; 

    double y1ans[800]; 
    double y2ans[800]; 
    double tans[800]; 


    Vector2d ycurr; 

    Vector2d Ynot, yplus; 

    Ynot << .2, 
      .1; 

    y1ans[0] = Ynot(0); 
    y2ans[0] = Ynot(1); 
    tans[0] = 0.0; 

    for(int i = 1;i<800;i++){ 
    tans[i] = tans[i-1] + h; 
    ycurr << y1ans[i-1], 
      y2ans[i-1]; 
    yplus = RK4(fitzhough,tans[i],ycurr,h,u,Iion,2); 
    y1ans[i] = yplus(0); 
    y2ans[i] = yplus(1); 
    } 
} 

addfitzhough.h(在單獨的文件)

#ifndef FF 
#define FF 

using namespace Eigen; 

Vector2d fitzhough(double t, Vector2d Y, double u, double * Iion); 

#endif // FITZ 

fitzhough.cpp

#include <Eigen/Dense> 

using namespace std; 
using namespace Eigen; 

Vector2d fitzhough(double t, Vector2d Y, double u, double * Iion) { 

    Vector2d dy; 

    double v = Y(0); 
    double w = Y(1); 

    double a = .13; 
    double b = .013; 
    double c1 = .26; 
    double c2 = .1; 
    double d = 1.0; 

    dy(0) = c1*v*(v-a)*(1-v)-c2*w*v + u; 
    dy(1) = b*(v-d*w); 

    *Iion = dy(0)-u; 

    return dy; 
} 
+1

什麼命令(或多個)您使用的編譯和鏈接代碼號碼:

順便說一句,如果你實現耦合FitzHugh - 南雲,他的名字的拼寫是耦合FitzHugh? –

+0

我正在編譯CodeBlocks。我沒有使用任何編譯器標誌 – user1968603

+0

@ user1968603您正在運行帶有選項的編譯器。一樣。 – dmckee

回答

0

它看起來您的構建系統不是構建fitzhough.cpp,或者不在鏈接階段添加fitzhough.o

如果您沒有,則應該將fitzhough.cpp添加到代碼塊項目(或同等項目)中。

+0

偉大的修復它!將它添加到項目中的做法與之前沒有做過的有什麼不同? – user1968603

+0

@ user1968603如果您不將它添加到項目中,則代碼塊不知道該文件是否存在並且應該編譯它。所以如果沒有編譯,鏈接器不知道'fitzhough'是什麼,因此是錯誤。 –

1

它看起來像你沒有鏈接到fitzhough.o(fitzhough.cpp的輸出)。您是否將fitzhough.cpp插入到項目中?