模板的聲明和定義分別在兩個(gè)文件中,其中function.h內(nèi)容為
#ifndef _BC_FUNCTION_H_
#define _BC_FUNCTION_H_
templateT add(const T &a,const T &b);
templateT mul(const T& a,const T& b);
templateT div(const T& a,const T& b);
templateT sub(const T& a,const T& b);
#include "../src/function.cpp"
#endif
function.cpp內(nèi)容為
#ifndef _FUNCTION_CPP_
#define _FUNCTION_CPP_
#include "../include/function.h"
templateT add(const T &a,const T &b)
{
return a+b;
}
templateT mul(const T &a,const T &b)
{
return a*b;
}
templateT div(const T& a,const T &b)
{
return a/b;
}
templateT sub(const T &a,const T& b)
{
return a-b;
}
#endif
如果定義類并且類中有static變量需要在類外初始化為
templateT* Foo::shared_resource_ = nullptr;
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧