2014-01-06 51 views
0

我無法使用頭文件包括的相伴約定使此代碼工作。C++:未定義的函數引用 - 如果包含頭文件並且包含源文件時失敗

help.cpp

#include <iostream> 
#include "basicMath.h" 



using namespace std; 

int main() { 

     int arr[4]={0,1,2,3}; 
     int k=0; 
     int m=3; 
     //permutations call 
     perm(arr,k,m); 
     return 0; 
} 

BasicMath.h

#ifndef BASICMATH_H_ 
    #define BASICMATH_H_ 


    template<class T> 
    void Swap (T& a, T& b); 

    template<class T1> 
    void perm (T1 arr[], int k, int m); 

    #endif /* BASICMATH_H_ */ 

BasicMath.cpp

#include <iostream> 
#include "basicMath.h" 

using namespace std; 

template<class T> 
void Swap (T& a, T& b) 
{ 
     T temp; 
     b=temp; 
     b=a; 
     a=temp; 
} 

template<class T1> 
void perm (T1 arr[], int k, int m) 
{ 
    //base case 
    cout << "Call: " << arr[0] << arr[1] << arr[2] << arr[3] << "\t" << k << "\t" << m << "\n"; 
    if (k==m) { 
       for (int i=0;i<=m;i++) { 
        cout << arr[i]; 
       } 
       cout << endl; 
    } else { 
      for (int i=k;i<=m;i++) { 
        swap(arr[k],arr[i]); 
        perm(arr,k+1,m); 
        swap(arr[k],arr[i]); 
       } 
    } 
} 

如果我用的#include取代的#include 「basicMath.h」「basicMath .cpp「然後程序工作。 請幫忙。 Eclipse項目使用標題和src新手。 感謝Adv。

+0

請在您的問題中包含編譯器和鏈接器的完整輸出。此外,這可能會變得相關:http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file –

回答