![]() |
#2
逐鹿2015-12-23 21:22
|
自己编了一个类头文件,在外部定义时引用出现了错误
总共三个文件:
Stack.h

#ifndef STACK_H
#define STACK_H
template<typename DataType> class Stack
{
public:
Stack(int size)
{
maxSize=size;
top=-1;
elements=new DataType[size];
}
~Stack()
{
delete[] elements;
}
bool push(DataType data);
DataType pop();
void pr(DataType data); //输出
private:
DataType *elements;
int top,maxSize;
};
#endif
Stack.cpp

#include <iostream>
#include "Stack.h"
/*******************入栈操作********************/
template<typename DataType > bool Stack<DataType>::push(DataType data)
{
if(top>=maxSize)
return false;
elements[++top]=data;
return true;
}
/*******************出栈操作******************/
template<typename DataType> DataType Stack<DataType>::pop()
{
if(top==-1)
{
exit(1);
}
return elements[top--];
}
/*********************输出*****************/
template<typename DataType> void Stack<DataType>::pr(DataType data) //输出
{
std::cout<<data<<ends;
}
main.cpp

/*************************************
一整数集合{23,56,11,4,87,98},
将他们依次存入某数据结构,
然后输出要求输出顺序为: 11,4,56,98,87,23.
*************************************/
#include <iostream>
#include "Stack.h"
using namespace std;
void main()
{
Stack<int> s=Stack<int>(6); //创建栈结构
int temp=0;
s.push(23);
s.push(56);
s.push(11);
s.pr(s.pop());
s.push(4);
s.pr(s.pop());
s.pr(s.pop());
s.push(87);
s.push(98);
s.pr(s.pop());
s.pr(s.pop());
s.pr(s.pop());
cout<<endl;
}
错误类型:
1>main.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall Stack<int>::pr(int)" (?pr@?$Stack@H@@QAEXH@Z),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: int __thiscall Stack<int>::pop(void)" (?pop@?$Stack@H@@QAEHXZ),该符号在函数 _main 中被引用
1>main.obj : error LNK2019: 无法解析的外部符号 "public: bool __thiscall Stack<int>::push(int)" (?push@?$Stack@H@@QAE_NH@Z),该符号在函数 _main 中被引用
1>E:\vs-program\VSproject\顺序表模板\Debug\栈模板.exe : fatal error LNK1120: 3 个无法解析的外部命令
如果将Stack.cpp中的代码直接复制到Stack.h中能正确运行。。
百度了很久也不知道怎么改,求指正,谢谢