本人刚接触C++有三个星期多,现在看到编写类摸板,方法摸板编写,目前遇到一个问题一直没得到解决的办法
类摸板编译全通过,但是连接出错误提示,请教出什么问题了?
下为全部代码
头文件:mobel.h
//**********************************
// mobel.h: interface for the mobel class.
//
//////////////////////////////////////////////////////////////////////
namespace mo{
#if !defined(AFX_MOBEL_H__B19737B3_F9C7_4153_A40A_E96E02351D34__INCLUDED_)
#define AFX_MOBEL_H__B19737B3_F9C7_4153_A40A_E96E02351D34__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//定义摸板类模块
static const int kWMax=10;
static const int kHMax=10;
template <typename MyT>
class mobel
{
public:
//mobel();
mobel(int inW=kWMax,int inH=kHMax);
mobel(const mobel<MyT>& src);
mobel<MyT>& operator=(const mobel<MyT>& rhs);
virtual ~mobel();
void setElement(int x,int y,const MyT& inElem);
MyT& getElement(int x,int y);
const MyT& getElement(int x,int y)const;
int getW()const {return mW;}
int getH()const {return mH;}
/*
static const int kWMax;
static const int kHMax;
*/
protected:
MyT** mTT;
int mW,mH;
private :
void copyFrom(const mobel<MyT>& src);
};
#endif // !defined(AFX_MOBEL_H__B19737B3_F9C7_4153_A40A_E96E02351D34__INCLUDED_)
}
//*************************************
//类定义:
// mobel.cpp: implementation of the mobel class.
//
//////////////////////////////////////////////////////////////////////
#include "mobel.h"
namespace mo{
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//摸板类的构造方法
template <typename MyT>
mobel<MyT>::mobel(int inW,int inH):
mW(inW),mH(inH)
{
mTT=new MyT* [mW];
for(int i=0;i<mW;i++)
mTT=new MyT[mH];
}
/*
//摸板类内的静态常量
template <typename MyT>
const int mobel<MyT>::kWMax=10;
//
template <typename MyT>
const int mobel<MyT>::kHMax=10;
*/
//copyFrom方法
template <typename MyT>
void mobel<MyT>::copyFrom(const mobel<MyT>& src)
{
int i,j;
mW=src.mW;
mH=src.mH;
mTT=new MyT* [mW];
for(i=0;i<mW;i++)
mTT=new MyT[mH];
for(i=0;i<mW;i++)
for(j=0;j<mH;j++)
mTT[j]=src.mTT[j];
}
//
template <typename MyT>
mobel<MyT>::mobel(const mobel<MyT>& src)
{
copyFrom(src);
}
//
template <typename MyT>
mobel<MyT>& mobel<MyT>::operator =(const mobel<MyT>& rhs)
{
if(this==&rhs)
return(*this);
for(int i=0;i<mW;i++)
delete [] mTT;
delete [] mTT;
//
copyFrom(rhs);
return (*this);
}
//
template <typename MyT>
mobel<MyT>::~mobel()
{
for(int i=0;i<mW;i++)
delete [] mTT;
delete [] mTT;
}
//
template <typename MyT>
void mobel<MyT>:: setElement(int x,int y,const MyT& inElem)
{
mTT[x][y]=inElem;
}
//
template <typename MyT>
MyT& mobel<MyT>::getElement(int x,int y)
{
return(mTT[x][y]);
}
//
template <typename MyT>
const MyT& mobel<MyT>::getElement(int x,int y)const
{
return(mTT[x][y]);
}
//end
}
//*****************************
//main :
#include <iostream>
#include <string>
#include "mobel.h"
using namespace std;
using namespace mo;
//定义方法
void proOfInteger(mobel<int> *myM);
void proOfDoubel(mobel<double> *myM);
/******************************
*方法名:proOfInteger
*作 用:测试Int类型参数引用摸板类
*参 数:*myM
******************************/
void proOfInteger(mobel<int> *myM)
{
//set the Values
myM->setElement(0,0,10);
myM->setElement(0,1,20);
myM->setElement(1,0,40);
myM->setElement(1,1,150);
//write out
cout<<"array[0][0]="<<myM->getElement(0,0)<<endl;
}
/******************************
*方法名:proOfDoubel
*作 用:测试double类型参数引用摸板类
*参 数:*myM
******************************/
void proOfDoubel(mobel<double> *myM)
{
int i=0,j=0,n=2;
//set the Values
myM->setElement(0,0,10.5);
myM->setElement(0,1,20.6);
myM->setElement(1,0,40.0);
myM->setElement(1,1,150.645);
//write out
for(;i<n;i++)
for(;j<n;j++)
cout<<"array["<<i<<"]["<<j<<"]="<<myM->getElement(i,j)<<endl;
}
//main function
int main(int argc, char **argv)
{
mobel<int>* MyMMAsInt=new mobel<int>();
mobel<double>* MyMMAsDouble=new mobel<double>();
proOfInteger(MyMMAsInt);
proOfDoubel(MyMMAsDouble);
delete MyMMAsInt;
delete MyMMAsDouble;
return 0;
}
//****************************************
//错误提示:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
mobel.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol "public: int & __thiscall mo::mobel<int>::getElement(int,int)" ([email=?getElement@?$mobel@H@mo@@QAEAAHHH@Z]?getElement@?$mobel@H@mo@@QAEAAHHH@Z[/email])
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall mo::mobel<int>::setElement(int,int,int const &)" ([email=?setElement@?$mobel@H@mo@@QAEXHHABH@Z]?setElement@?$mobel@H@mo@@QAEXHHABH@Z[/email])
main.obj : error LNK2001: unresolved external symbol "public: double & __thiscall mo::mobel<double>::getElement(int,int)" ([email=?getElement@?$mobel@N@mo@@QAEAANHH@Z]?getElement@?$mobel@N@mo@@QAEAANHH@Z[/email])
main.obj : error LNK2001: unresolved external symbol "public: void __thiscall mo::mobel<double>::setElement(int,int,double const &)" ([email=?setElement@?$mobel@N@mo@@QAEXHHABN@Z]?setElement@?$mobel@N@mo@@QAEXHHABN@Z[/email])
main.obj : error LNK2001: unresolved external symbol "public: __thiscall mo::mobel<double>::mobel<double>(int,int)" ([email=??0?$mobel@N@mo@@QAE@HH@Z]??0?$mobel@N@mo@@QAE@HH@Z[/email])
main.obj : error LNK2001: unresolved external symbol "public: __thiscall mo::mobel<int>::mobel<int>(int,int)" ([email=??0?$mobel@H@mo@@QAE@HH@Z]??0?$mobel@H@mo@@QAE@HH@Z[/email])
Debug/main.exe : fatal error LNK1120: 6 unresolved externals
Error executing link.exe.
main.exe - 7 error(s), 0 warning(s)
大家看一下啥回事??