vs2005编译出错求解
我想用操作符重载输入一个堆栈,其他的代码省略了,但在VS2005下出现了下列错误,请高手帮忙解决一下。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template <class T>
class Stack
{
friendostream& operator
<< (ostream&, const Stack<T>&);
friendistream& operator
>>(istream&, Stack<T>&);
public:
Stack(int MaxStackSize =
10);
~Stack(){delete[] stack;}
bool IsEmpty() const
{return top == -1;}
bool IsFull() const {return
top ==MaxTop;}
int Size() const {return
Top+1;}
void Input();
T top() const;
Stack<T>& Add(const T& x);
Stack<T>& Delete(T& x);
private:
int Top;
int MaxTop;
T *stack;
};
template<class T>
istream& operator>>(istream& in,
Stack<T>& s)
{
int size;
cout << "Enter size of stack" <<
endl;
in >> size;
if (size > s.MaxTop + 1) return;
// fail
s.top = size - 1;
cout << "Enter the elements from
top to bottom"<< endl;
for (int i = s.top; i >= 0 ; i-
-)
{
in >> s.stack[i];
}
return in;
}
template <class T>
ostream& operator<<(ostream&
out,const Stack<T>& s)
{
out << "The stack has " <<
(s.top + 1)<< " element(s)" <<
endl;
out << "The element(s) from top
to bottom are"<< endl;
for (int i = s.top; i >= 0 ; i-
-)
{
out << s.stack[i] << ' ';
}
out << endl;
return out;
}
int main()
{
Stack<int> s;
cin>>s;
return 0;
}
error: BaseStack.obj : error
LNK2019: 无法解析的外部符号 "class
std::basic_ostream <char,struct
std::char_traits <char> > & __cdecl
operator >>(class
std::basic_ostream <char,struct
std::char_traits <char> > &,class
Queue <int> const &)" (??6@YAAAV?
$basic_ostream@DU?
$char_traits@D@std@@@std@@AAV01@ABV
?$Queue@H@@@Z),该符号在函数 _main
中被引用
[解决办法]
模板声明放在.h、.hpp里面