关于函数模板
1.文件common.h
#ifndef COMMON_H
#define COMMON_H
template <typename type>
void swap(type *a, type *b);
#endif
2.文件common.cpp
#include "common.h"
template <typename type>
void swap(type *a, type *b)
{
type temp;
temp = *a;
*a = *b;
*b = temp;
}
3.文件main.cpp
#include "common.h"
using namespace std;
int main(int argc, char *argv[])
{
int data[] = {9,6};
swap<int>(&data[0], &data[1]);
system("PAUSE");
return 0;
}
编译时老报错: [Linker error] undefined reference to `void swap<int>(int*, int*)'
请问大家:函数模板的定义和实现是不是不能分开?
谢谢了