类模板问题~unable to resolve function overload
谢谢大家帮忙解决问题~
#include<iostream>
using namespace std;
template<class type>
class array
{
public:
array(int n)
{
element=new type[n];
size=n;
}
~array()
{ delete element;}
void operator =(type x);
type &operator [](int index);
friend ostream &operator <<(ostream&os,array<type>&a);
private:
type *element;
int size;
};
template<class type>
type array<type>::operator [](int index)
{
return *(element+index);
}
template<class type>
void array<type>::operator=(type x)
{
for(int i=0;i<size;i++)
*(element+i)=x;
}
template<class type>
ostream &operator<<(ostream&os,array<type>&a)
{
for(int i=0;i<a.size;i)
os<<*(a.element+i)<<" ";
os<<endl;
return os;
}
int main()
{
array<int>iobj(3);
array<double>dobj(2);
array<char>cobj(4);
iobj=364;
dobj=1.414;
cobj='A';
cout<<"integer array:\n"<<iobj;
cout<<"double array:\n"<<dobj;
cout<<"char array:\n"<<cobj;
return 0;
}
--------------------Configuration: 9_9 - Win32 Debug--------------------
Compiling...
9_9.cpp
D:\c++\1\课本\9_9.cpp(27) : error C2244: 'array<type>::[]' : unable to resolve function overload
D:\c++\1\课本\9_9.cpp(29) : error C2954: template definitions cannot nest
执行 cl.exe 时出错.
9_9.obj - 1 error(s), 0 warning(s)
[解决办法]
编译器: Default compiler
执行 g++.exe...
执行结束
成功编译