简单模板应用出错(帮帮忙)
哪位朋友帮忙看下程序,用VC编译的,错误那行后面用注释标出来了#include<iostream>
using namespace std;
class Animal
{
public:
Animal():itsAge(0){}
Animal(int Age):itsAge(Age){}
~Animal(){}
int GetAge()const{return itsAge;}
int SetAge(int Age){itsAge=Age;}
friend ostream & operator <<(ostream &,const Animal &);
private:
int itsAge;
};
ostream & operator <<(ostream & output,const Animal & theAnimal)
{
output<<theAnimal.GetAge();
return output;
}
template <class Type>
class Array
{
public:
Array(int Size=10);
Array(const Array & );
~Array(){ delete [] ptr;}
Array & operator =(const Array &);
Type & operator [](int offset){return ptr[offset];}
const Type & operator [](int offset)const{return ptr[offset];}
int GetSize()const{return itsSize;}
friend ostream& operator <<(ostream &, const Array<Type>&);
Type * GetPtr()const{return ptr;}
private:
int itsSize;
Type * ptr;
};
template <class Type>
Array<Type>::Array(int Size)
{
itsSize=Size;
ptr=new Type[Size];
for(int i=0;i<Size;i++)
ptr[i]=0;
}
template <class Type>
Array<Type>::Array(const Array & target)
{
itsSize=target.GetSize();
ptr=new Type[itsSize];
for(int i=0;i<itsSize;i++)
ptr[i]=target[i];
}
template <class Type>
Array<Type>& Array<Type>::operator =(const Array &target)
{
if(this==&target)
return *this;
delete []ptr;
itsSize=target.GetSize();
ptr=new Type[itsSize];
for(int i=0;i<itsSize;i++)
ptr[i]=target[i];
return *this;
}
template <class Type>
ostream& operator<<(ostream & output, const Array<Type>& theArray)
{
for(int i=0;i<theArray.GetSize();i++)
output<<"["<<i<<"]="<<theArray[i]<<endl; //这一行编译通不过,此乃第71行
return output;
}
void intFillArray(Array<int>& theArray)
{
cout<<"Please enter the values to fill the int array...\n";
int m;
for(int i=0;i<theArray.GetSize();i++)
{
cout<<"int["<<i<<"]=";
cin>>m;
theArray[i]=m;
}
}
void animalFillArray(Array<Animal> & theAnimal)
{
cout<<"Please enter the values to fill the animal array...\n";
Animal *ptrAnimal;
for(int i=0;i<theAnimal.GetSize();i++)
{
ptrAnimal=new Animal;
ptrAnimal->SetAge(i*100);
theAnimal[i]=*ptrAnimal;
delete ptrAnimal;
}
}
//driver program
int main()
{
Array<int> intArray;
Array<Animal> animalArray;
intFillArray(intArray);
animalFillArray(animalArray);
cout<<"***this is the int array***\n";
cout<<intArray;
cout<<"***this is the animal array***\n";
cout<<animalArray; //如果去掉这一行则可以运行...这是第112行
getchar();
return 0;
}
错误提示如下:Cpp1.cpp(71) : error C2593: 'operator <<' is ambiguous
Cpp1.cpp(112) : see reference to function template instantiation 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl operator <<(class std::basic_ostream<char,struct std::char_
traits<char> > &,const class Array<class Animal> &)' being compiled