I am a beginner with c++, and I cannot run the program while I can sucessfully compile the program. Here are my code:
templateclass.cpp
-----------------------------------------------------------------------------
#include "templateclass.h"
#include <iostream>
using namespace std;
template <class mix>
void InsetionArray <mix>::input(mix b[])
{
for (int i=0; i<10; i++)
{ a[i] = b[i]; }
}
template <class mix>
void InsetionArray <mix>::output()
{
for (int i=0; i<10; i++)
{ cout << a[i] << endl; }
cout << endl;
}
template <class mix>
void InsetionArray <mix>::insertion()
{
int i, j;
for (i = 1; i < 10; ++i)
{
j=i;
while (j>0 && a[j-1]>a[j])
{
my_swap (a[j-1], a[j]);
j--;
}
}
}
template <class mix>
void InsetionArray <mix>::my_swap(mix& x, mix& y)
{
mix tmp;
tmp = x;
x = y;
y = tmp;
}
------------------------------------------------------------------------
templateclass.h
------------------------------------------------------------------------
#ifndef TEMPLATECLASS_H
#define TEMPLATECLASS_H
#include <iostream>
using namespace std;
template <class mix>
class InsetionArray
{
public:
// InsetionArray();
void input(mix a[]);
void output();
void insertion();
void my_swap(mix&, mix&);
private:
mix a[10];
};
#endif
-------------------------------------------------------------------------------
main.cpp
-----------------------------------------------------------------------------
#include <iostream>
#include "templateclass.h"
using namespace std;
void main()
{
int a[10];
for( int i=0; i<10; i++)
{
cin >> a[i];
}
InsetionArray<int> x;
x.input(a);
x.insertion();
x.output();
}
----------------------------------------------------------------------------
What's wrong with my program?
thank you!!
the template functions cannot go in a separate cpp file. They must all be in the header file.