关于C++ 类模板

来源:百度知道 编辑:UC知道 时间:2024/06/01 08:55:36
ctt.h
template <typename CSTYPE>
class Cs{
public:
Cs();
~Cs();
CSTYPE t(Cs<CSTYPE> &g);
friend Cs<CSTYPE> operator + (Cs<CSTYPE> &, CSTYPE &);
protected:
CSTYPE m_type;
};
////////////////////////////////////
ctt.cpp
#include "stdafx.h"
#include "ctt.h"
template <typename CSTYPE> Cs<CSTYPE>::Cs()
{
m_type = 0;
}
template <typename CSTYPE> Cs<CSTYPE>::~Cs(){}
template <typename CSTYPE> CSTYPE Cs<CSTYPE>::t(Cs<CSTYPE> &g)
{
g.m_type = 123;
return g.m_type;
}
template <typename CSTYPE> Cs<CSTYPE> operator + (Cs<CSTYPE> &o1, CSTYPE &o2)
{
Cs<CSTYPE> temp;
temp.m_type = o1.m_type + o2;
return temp;
}
////////////////////////////////////
mainf.cpp

#include "stdafx.h"
#include "ctt.h&q

让我告诉你
像下面这样的模板定义 一定要放在头文件中
template <typename CSTYPE> Cs<CSTYPE>::Cs()
{
m_type = 0;
}
头文件中有所有 template
不能放在cpp文件中

这样就没错误了啊

template <typename CSTYPE>
class Cs{
public:
Cs();
~Cs();
CSTYPE t(Cs<CSTYPE> &g);
friend Cs<CSTYPE> operator + (Cs<CSTYPE> &, CSTYPE &);
protected:
CSTYPE m_type;
};

template <typename CSTYPE> Cs<CSTYPE>::Cs()
{
m_type = 0;
}
template <typename CSTYPE> Cs<CSTYPE>::~Cs(){}
template <typename CSTYPE> CSTYPE Cs<CSTYPE>::t(Cs<CSTYPE> &g)
{
g.m_type = 123;
return g.m_type;
}
template <typename CSTYPE> Cs<CSTYPE> operator + (Cs<CSTYPE> &o1, CSTYPE &o2)
{
Cs<CSTYPE> temp;
temp.m_type = o1.m_type + o2;
return temp;
}

int main(int argc, char* argv[])
{
Cs