64,676
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
const int MAX=5;
template <class T>
class Z
{
private:
int top;
T s[MAX];
public:
Z(){top=-1;}
void push(const T& input);
T pop();
int emptystack()const;
};
template <class T>
void Z<T>::push(const T& input)
{
if(top==MAX-1)
{
cout <<"栈满溢出" <<endl;
exit(1);
}
top++;
s[top]=input;
}
template <class T>
T Z <T>::pop()
{
T temp;
if(top==-1)
{
cout <<"栈为空" <<endl;
exit(1);
}
temp=s[top];
top--;
return temp;
}
template <class T>
int Z <T>::emptystack()const
{
return top==-1;
}
int main()
{
Z<int> st1;
int a[]={4,8,3,2};
for(int i=0;i<4;i++)
{
st1.push(a[i]);
}
while(!st1.emptystack())
cout <<st1.pop();
system("pause");
return 0;
}
Z.h:
#ifndef Z_H
#define Z_H
#include <iostream>
using namespace std;
const int MAX=5;
template <class T>
class Z
{
private:
int top;
T s[MAX];
public:
Z(){top=-1;}
void push(const T& input);
T pop();
int emptystack()const;
};
#endif
-----------------------------------------------
z.cpp:
#include"Z.h"
#include <iostream>
using namespace std;
template <class T>
void Z <T>::push(const T& input)
{
if(top==MAX-1)
{
cout < <"栈满溢出" < <endl;
exit(1);
}
top++;
s[top]=input;
}
template <class T>
T Z <T>::pop()
{
T temp;
if(top==-1)
{
cout < <"栈为空" < <endl;
exit(1);
}
temp=s[top];
return temp;
}
template <class T>
int Z <T>::emptystack()const
{
return top==-1;
}
-------------------------------------
zmain.cpp:
#include"Z.h"
#include <iostream>
using namespace std;
int main()
{
Z <int> st1;
int a[]={4,8,3,2};
for(int i=0;i <4;i++)
{
st1.push(a[i]);
}
while(!st1.emptystack())
cout < <st1.pop();
system("pause");
return 0;
}
-----------------------------
编译错误:
[Linker error] undefined reference to `Z <int>::push(int const&)'
[Linker error] undefined reference to `Z <int>::emptystack() const'
[Linker error] undefined reference to `Z <int>::pop()'
ld returned 1 exit status
D:\Program Files\DEV-CPP\Makefile.win [Build Error] [工程1.exe] Error 1
小辈初学c++,请高手指教,谢谢啦