0

I'm not able to make this code work using fellowed convention of header includes.

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]);
              }
     }
}

IF I replace the #include "basicMath.h" by #include "basicMath.cpp" Then program works. Please help. New to Eclipse Project using headers and src. Thanks in Adv.

  • Please include the full output of the compiler and linker in your question. Also, this may become relevant: http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – Chris Culter Jan 06 '14 at 04:25

1 Answers1

0

The "simple" answer is that you can't put the implementation of a template function into a .cpp file.

See http://www.parashift.com/c++-faq/templates-defn-vs-decl.html

Chris Walton
  • 1,326
  • 8
  • 12