报错信息
1>test.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class test const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$test@H@@@Z),该符号在函数 _main 中被引用
1>c:\users\lenovo\documents\visual studio 2010\Projects\test1\Debug\test1.exe : fatal error LNK1120: 1 个无法解析的外部命令
小弟是初学者,代码可能还有各种小问题,希望各位大大们都能帮忙一并指出,感激不尽
代码如下
#include<iostream>
using namespace std;
template<class T>
class test{
private:
int sizeOfArray;
T* element;
public:
test(int,int);
~test();
friend ostream& operator<<(ostream&,const test<T>&);
};
template<class T>
test<T>::test(int size,int startValue){
sizeOfArray=size;
element=new T[sizeOfArray];
for(int i=0;i<sizeOfArray;i++){
element[i]=startValue+i;
}
}
template<class T>
test<T>::~test(){
delete[] element;
}
template<class T>
ostream& operator<<(ostream& out,const test<T>& x){
for(int i=0;i<sizeOfArray;i++){
out<<element[i]<<" ";
}
return out;
}
int main(){
test<int> a(3,0);
cout<<a;
system("pause");
return 0;
}