模板的编译问题
wu_wo 2007-08-06 01:10:13 小弟写的一个简单的程序,来验证C++ Primer上说的当模板的声明和定义在不同文件中时,可以采用包含编译模型来编译,但是怎么有错误.程序如下:
//print.h
#ifndef PRINT_H
#define PRINT_H
#include<iostream>
using namespace std;
template<class T> class print
{
public:
print(T);
void display();
private:
T data;
};
#include"print.cpp"
#endif
//print.cpp
#include"print.h"
template<class T> print<T>::print(T data0):data(data0)
{}
template<class T> void print<T>::display()
{
cout<<data<<endl;
}
//main.cpp
#include"print.h"
void main()
{
print<double> a(3.14);
a.display();
}