标题:简单模板应用出错(帮帮忙)
取消只看楼主
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
 问题点数:0 回复次数:4 
简单模板应用出错(帮帮忙)
哪位朋友帮忙看下程序,用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
搜索更多相关主题的帖子: Animal int itsAge 模板 
2008-06-01 13:31
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
得分:0 
二楼和三楼的方法我试了还是不对哦
2008-06-01 18:27
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
得分:0 
5楼的那样改可以运行了,谢谢啦!!!
还有辛苦2楼和3楼了,嘻嘻!
2008-06-01 20:54
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
得分:0 
再问下5楼,我重新看了你的程序,好像就多加了个命名空间组而已,是吗?还有没有改其它的地方。搞不懂为什么一定要加个命名空间组才行呢
2008-06-01 21:24
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
得分:0 
哦,这样子啊,谢了!
2008-06-02 13:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-217005-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.364985 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved