用C++类模板实现栈结构出现的问题以及思考
C++中使用了模板来减少方法相同但是类型不一样带来的函数重载以及大量复制代码的问题。这里主要说说类模板
类模板的定义:
template<TYPENAME Type>
class Stacks
{
public:
Stacks(void);
Stacks(int nSize);
Stacks(Type Arr[],int nSize);
~Stacks(void);
public:
bool isEmpty();
bool isFull();
bool push(const Type &e);
Type pop();
Type getTop();
int getLength();
void print();
private:
int m_top;
int m_nLength;
Type *m_pArr;
};
函数的实现:
函数实现既可以放在类定义体内,又可以放在类定义体外,但是不管放在体内还是体外都必须放在头文件中,这点和inline函数有点类似。
分离编译托福答案 www.yztrans.com
如果声明和定义分别放在h文件和cpp文件呢?
这个时候如果在其他程序中用到了模板类中的函数,那么就会出现
然后我们在main中调用print函数,在生成的debug文件中用notepad++打开main.obj,我们可以看到有这样的字符出现:
www.lefeng123.com
这就说明了问题,main.cpp中实例化了函数的定义。
附带栈结构实现的C++源代码
// Stacks.h
#pragma once
#include <IOSTREAM>
using namespace std;
template<TYPENAME Type>
class Stacks
{
public:
Stacks(void);
Stacks(int nSize);
Stacks(Type Arr[],int nSize);
~Stacks(void);
public:
bool isEmpty();
bool isFull();
bool push(const Type &e);
Type pop();
Type getTop();
int getLength();
void print();
private:
int m_top;
int m_nLength;
Type *m_pArr;
};
template<TYPENAME Type>
Stacks<TYPE>::Stacks(void)
{
m_top = -1;
m_nLength = 10;
m_pArr = new Type[10];
if (m_pArr == NULL)
cout《"Allocate stack failed"《ENDL; Type } template<typename m_top="m_nLength-1;" else>
Stacks<TYPE>::Stacks(int nSize)
{
m_top = -1;
m_pArr = new Type[nSize];
if(m_pArr == NULL)
cout《"Allocate stack failed"《ENDL; Type } template<typename m_top="nSize-1;" else m_nLength="nSize;" {>
Stacks<TYPE>::Stacks(Type Arr[],int nSize)
{
m_top = -1;
m_pArr = new Type[nSize];
if(m_pArr == NULL)
cout《"Allocate stack failed"《ENDL; Type } template<typename m_top="nSize" else m_nLength="nSize;" { 1; - m_pArr[i]="Arr[i];" i="0;i<nSize;i++)" for(int>
bool Stacks<TYPE>::isEmpty()
{
return m_top == -1;
}
template<TYPENAME Type>
bool Stacks<TYPE>::isFull()
{
return m_top == m_nLength - 1;
}
template<TYPENAME Type>
Type Stacks<TYPE>::getTop()
{
if (m_top != -1)
return m_pArr[m_top];
else
{
cout《"The stack is empty"《ENDL; Type } template<typename exit(1);>
Type Stacks<TYPE>::pop()
{
if(m_top == -1)
{
cout《"The stack is empty"《ENDL; Type } template<typename else { exit(1); m_pArr[m_top+1]; return m_top--;>
bool Stacks<TYPE>::push(const Type &e)
{
if(m_top == m_nLength-1)
{
cout《"The stack is full"《ENDL; Type } template<typename else { return true; m_pArr[m_top]="e;" m_top++; false;>
int Stacks<TYPE>::getLength()
{
return m_nLength;
}
template<TYPENAME Type>
Stacks<TYPE>::~Stacks(void)
{
if(m_pArr != NULL)
{
delete m_pArr;
m_pArr = NULL;
}
}
template<TYPENAME Type>
void Stacks<TYPE>::print()
{
cout《"This is a test ,I love you"
《"I love you,I love you"《ENDL; class="brush":java; }<pre>
//main.cpp
#include<IOSTREAM>
#include<STRING>
#include"Stacks.h"
using namespace std;
void main()
{
int a[7] = {1,2,3,4,5,6,7};
Stacks<INT> Sta(a,7);
cout《STA.ISFULL()《ENDL; pre } { i="0;i<7;i++)" for(int }< system(?pause?); Sta.print(); cout《Sta.getLength()《endl; cout《Sta.isEmpty()《endl; is:?《top《endl; top="Sta.pop();" cout《?The int><BR>
<P></P>
<P><BR>
</P>
<P><BR>
</P>
<P><BR>
</P>
<BR>
<BR>