test.cpp:=================================
#include <iostream>
#include "test_math.h"
using namespace std;
int main(void)
{
cout << getmax<int>(12, 21) << endl;
return 0;
}
test_math.h:==============================
#ifndef TEST_MATH_H_
#define TEST_MATH_H_
template <class T> T getmax(T a, T b);
#endif
test_math.cpp:=============================
#include "test_math.h"
template <class T> T getmax(T a, T b)
{
return (a > b) ? a : b;
}
编译:
g++ -otest test.cpp test_math.cpp
test.cpp: undefined reference to 'int getmax<int>(int, int)'
但是如果把它们都放在同一个文件就不会发生这样的事。
有人知道为什么吗? |