这是代码:无关的函数没有发:#include <afxtempl.h>template<class TYPE>class CMatrix3D : public CObject
{
DECLARE_SERIAL(CMatrix3D)
public:
CMatrix3D();
CMatrix3D(const int Nz,const int Ny, const int Nx);
virtual ~CMatrix3D(); virtual BOOL Create(const int Nz, const int Ny, const int Nx, const bool bDataInMemory = true);
virtual BOOL Create(const int Ny, const int Nx, const bool bDataInMemory = true); float m_invalid;
BOOL m_bHasInvalid;protected:
CArray<TYPE, TYPE&> *m_pData;
int m_nx, m_ny, m_nz;
};
template<typename TYPE>
BOOL CMatrix3D<TYPE>::Create(const int Nz,const int Ny, const int Nx, const bool bDataInMemory/* = true*/)
{
// 其他代码已经删除
TYPE t = (TYPE)m_invalid; return TRUE;
}问题: 我调用的时候:
CMatrix3D<float> oMatrix ; 或者
CMatrix3D<float> * pMatrix = new CMatrix3D<float> ();
//都出错 error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CMatrix3D<float>::GetRuntimeClass(void)const " (?GetRuntimeClass@?$CMatrix3D@M@@UBEPAUCRuntimeClass@@XZ)
Debug/ma.exe : fatal error LNK1120: 1 unresolved externals
另外的一种调用方法:
class haha {} ;
CMatrix3D<haha > * pMatrix = new CMatrix3D<haha > () ;
出错的是:
Create函数:TYPE t = (TYPE)m_invalid;
error C2440: 'type cast' : cannot convert from 'float' to 'class haha'
No constructor could take the source type, or constructor overload resolution was ambiguous我的程序应用的是第一种调用方法,那位知道我的调用什么地方出错了??我已经郁闷了一个星期了。
{
DECLARE_SERIAL(CMatrix3D)
public:
CMatrix3D();
CMatrix3D(const int Nz,const int Ny, const int Nx);
virtual ~CMatrix3D(); virtual BOOL Create(const int Nz, const int Ny, const int Nx, const bool bDataInMemory = true);
virtual BOOL Create(const int Ny, const int Nx, const bool bDataInMemory = true); float m_invalid;
BOOL m_bHasInvalid;protected:
CArray<TYPE, TYPE&> *m_pData;
int m_nx, m_ny, m_nz;
};
template<typename TYPE>
BOOL CMatrix3D<TYPE>::Create(const int Nz,const int Ny, const int Nx, const bool bDataInMemory/* = true*/)
{
// 其他代码已经删除
TYPE t = (TYPE)m_invalid; return TRUE;
}问题: 我调用的时候:
CMatrix3D<float> oMatrix ; 或者
CMatrix3D<float> * pMatrix = new CMatrix3D<float> ();
//都出错 error LNK2001: unresolved external symbol "public: virtual struct CRuntimeClass * __thiscall CMatrix3D<float>::GetRuntimeClass(void)const " (?GetRuntimeClass@?$CMatrix3D@M@@UBEPAUCRuntimeClass@@XZ)
Debug/ma.exe : fatal error LNK1120: 1 unresolved externals
另外的一种调用方法:
class haha {} ;
CMatrix3D<haha > * pMatrix = new CMatrix3D<haha > () ;
出错的是:
Create函数:TYPE t = (TYPE)m_invalid;
error C2440: 'type cast' : cannot convert from 'float' to 'class haha'
No constructor could take the source type, or constructor overload resolution was ambiguous我的程序应用的是第一种调用方法,那位知道我的调用什么地方出错了??我已经郁闷了一个星期了。
可能使我原来遇到的一个问题要把实现部分放在头文件里面
即没有cpp文件
float m_invalid;是不是应该是TYPE m_invalid
// RENJUN WEEN, ALL RIGHT RESERVED, 14-MAY-1996
// --------------------------------------------#ifndef __MATRIX3D_H__
#define __MATRIX3D_H__#include <afxtempl.h>template<class TYPE>class CMatrix3D : public CObject
{
DECLARE_SERIAL(CMatrix3D)
public:
CMatrix3D();
CMatrix3D(const int Nz,const int Ny, const int Nx);
virtual ~CMatrix3D(); CMatrix3D<TYPE>& operator = (const CMatrix3D<TYPE>& A);
CMatrix3D<TYPE>& operator = (const TYPE A);
TYPE& operator() (const int Iz,const int Iy, const int Ix);
const TYPE operator() (const int Iz,const int Iy, const int Ix) const; virtual BOOL Create(const int Nz, const int Ny, const int Nx, const bool bDataInMemory = true);
virtual BOOL Create(const int Ny, const int Nx, const bool bDataInMemory = true); enum SliceType {SHARED, COPIED};
void SetAll(const TYPE A);
void DeleteData(); int Nz() const;
int Ny() const;
int Nx() const; TYPE *GetData() // Return the pointer to the matrix data
{
if (m_pData == NULL ) return NULL;
else return m_pData->GetData() ;
} TYPE *GetData() const // Return the pointer to the matrix data
{
if (m_pData == NULL ) return NULL;
else return m_pData->GetData() ;
} virtual void Serialize(CArchive& ar);
virtual void AssertValid() const;
virtual void FindMinMax(TYPE &Min, TYPE &Max); float m_invalid;
BOOL m_bHasInvalid;protected:
CArray<TYPE, TYPE&> *m_pData;
int m_nx, m_ny, m_nz;
BOOL m_bOwer; // If this matrix is the ower of m_pData
BOOL m_bInMemory;
};template<class TYPE>
inline void CMatrix3D<TYPE>::SetAll(const TYPE A)
{
TYPE *pData = GetData();
size_t n = (size_t)m_nx*m_ny*m_nz;
for (size_t i = 0; i < n; i++)
pData[i] = A;
}template<class TYPE>
inline int CMatrix3D<TYPE>::Nz() const
{
return m_nz;
}template<class TYPE>
inline int CMatrix3D<TYPE>::Ny() const
{
return m_ny;
}template<class TYPE>
inline int CMatrix3D<TYPE>::Nx() const
{
return m_nx;
}template<class TYPE>
inline TYPE& CMatrix3D<TYPE>::operator() (const int Iz,const int Iy, const int Ix)
{
#ifdef _BOUNDCHECK
ASSERT_VALID(this);
ASSERT(Ix >= 0 && Ix < m_nx);
ASSERT(Iy >= 0 && Iy < m_ny);
ASSERT(Iz >= 0 && Iz < m_nz);
#endif
// A 3D matrix is organized as a sequence of 2D matrix in xy plane
// The order of change, x, y, z
return (*m_pData)[Iz*m_nx*m_ny + Iy*m_nx + Ix];
}template<class TYPE>
inline const TYPE CMatrix3D<TYPE>::operator() (const int Iz,const int Iy, const int Ix) const
{
#ifdef _BOUNDCHECK
ASSERT_VALID(this);
ASSERT(Ix >= 0 && Ix < m_nx);
ASSERT(Iy >= 0 && Iy < m_ny);
ASSERT(Iz >= 0 && Iz < m_nz);
#endif
// A 3D matrix is organized as a sequence of 2D matrix in xy plane
// The order of change, x, y, z
return (*m_pData)[Iz*m_nx*m_ny + Iy*m_nx + Ix];
}template<class TYPE>
inline CMatrix3D<TYPE>& CMatrix3D<TYPE>::operator = (const CMatrix3D<TYPE>& A)
{
if (this == &A)
return *this;
Create(A.Nz(), A.Ny(), A.Nx());
for (iz = 0; iz < m_nz; iz++)
for (iy = 0; iy < m_ny; iy++)
for (ix = 0; ix < m_nx; ix++)
(*this)(iz,iy,ix) = A(iz,iy,ix);
m_invalid = A.m_invalid;
m_bHasInvalid = A.m_bHasInvalid;
return *this;
}template<class TYPE>
inline CMatrix3D<TYPE>& CMatrix3D<TYPE>::operator = (const TYPE A)
{
ASSERT_VALID(this);
int n = m_pData->GetSize();
for (int i = 0; i < n; i++)
(*m_pData)[i] = A;
return *this;
}template<class TYPE>
CMatrix3D<TYPE>::CMatrix3D()
{
m_pData = NULL;
m_nx = m_ny = m_nz = 0;
m_bOwer = TRUE;
m_invalid = -99999;
m_bHasInvalid = TRUE;
m_bInMemory = FALSE;
}template<class TYPE>
CMatrix3D<TYPE>::CMatrix3D(const int Nz,const int Ny, const int Nx)
{
ASSERT(Nx > 0);
ASSERT(Ny > 0);
ASSERT(Nz > 0);
m_nx = Nx;
m_ny = Ny;
m_nz = Nz;
m_pData = new CArray<TYPE, TYPE&>;
m_pData->SetSize(m_nx*m_ny*m_nz);
m_bOwer = TRUE;
m_invalid = -99999;
m_bHasInvalid = TRUE;
TYPE t = m_invalid;
for (int i=0; i < m_pData->GetSize(); i++)
m_pData->SetAt(i, t);
m_bInMemory = TRUE;
}template<class TYPE>
CMatrix3D<TYPE>::~CMatrix3D()
{
if (m_pData != NULL && m_bOwer)
{
m_pData->RemoveAll();
delete m_pData;
m_pData = NULL;
}
}template<class TYPE>
void CMatrix3D<TYPE>::DeleteData()
{
if (m_pData != NULL && m_bOwer)
{
m_pData->RemoveAll();
delete m_pData;
m_pData = NULL;
}
m_bInMemory = FALSE;
}template<class TYPE>
BOOL CMatrix3D<TYPE>::Create(const int Nz,const int Ny, const int Nx, const bool bDataInMemory/* = true*/)
{
ASSERT(Nx > 0);
ASSERT(Ny > 0);
ASSERT(Nz > 0);
if (Nx <= 0 || Ny <= 0 || Nz <= 0)
{
AfxMessageBox("Error to create a matrix. Invalid dimension!");
return false;
}
if (m_pData != NULL && m_bOwer)
{
m_pData->RemoveAll();
delete m_pData;
}
m_nx = Nx;
m_ny = Ny;
m_nz = Nz;
m_bOwer = bDataInMemory;
m_bInMemory = bDataInMemory; if (bDataInMemory)
{
m_pData = new CArray<TYPE, TYPE&>;
m_pData->SetSize(m_nx*m_ny*m_nz);
TYPE t = (TYPE)m_invalid;
m_bHasInvalid = TRUE;
for (int i=0; i < m_pData->GetSize(); i++)
m_pData->SetAt(i, t); }
return TRUE;
}template<class TYPE>
BOOL CMatrix3D<TYPE>::Create(const int Ny, const int Nx , const bool bDataInMemory/* = true*/)
{
return Create(1, Ny, Nx, bDataInMemory);
}template<class TYPE>
void CMatrix3D<TYPE>::Serialize(CArchive& ar)
{
//ASSERT_VALID(this);
// CGmObject::Serialize(ar);
if (ar.IsStoring())
{
ar << (LONG)m_nx << (LONG)m_ny << (LONG)m_nz << m_invalid << (LONG)m_bHasInvalid
<< m_bInMemory; }
else
{
ar >> (LONG&)m_nx >> (LONG&)m_ny >> (LONG&)m_nz >> m_invalid >>(LONG&)m_bHasInvalid
>> m_bInMemory;
m_bOwer = TRUE;
if(m_bInMemory)
{
m_pData = new CArray<TYPE, TYPE&>;
m_pData->SetSize(m_nx*m_ny*m_nz);
}
}
if(m_bInMemory && m_pData != NULL)
m_pData->Serialize(ar);
}template<class TYPE>
void CMatrix3D<TYPE>::AssertValid() const
{
CObject::AssertValid();
ASSERT(m_pData != NULL);
m_pData->AssertValid();
}template<class TYPE>
void CMatrix3D<TYPE>::FindMinMax(TYPE &Min, TYPE &Max)
{
TYPE *x = GetData();
if (!x) return;
UINT n = m_nx*m_ny*m_nz;
Min = Max = x[0];
for (UINT i = 1; i < n; i++)
{
if (x[i] < Min)
Min = x[i];
if (x[i] > Max)
Max = x[i];
}
}#endif
以上面的为例
IMPLEMENT_DYNMIC(CMatrix3D,CObject);
是这样的吗