晨曦的朝阳 发表于 2008-6-1 13:31

简单模板应用出错(帮帮忙)

哪位朋友帮忙看下程序,用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

zhengqiang_hust 发表于 2008-6-1 13:45

应该是这样

#include<iostream>
using namespace std;
class Animal
{
      friend ostream & operator << (ostream & output,const Animal & theAnimal);
      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 <Type> <<(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<< <Type> (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;
}

中学者 发表于 2008-6-1 14:24

VC6.0的问题....改成这样试试:
template<typename _Ty>
friend ostream& operator<<(ostream &, const Array<_Ty>&);
template <typename _Ty>
ostream& operator<<(ostream & output, const Array<_Ty>& theArray)
{
         for(int i=0;i<theArray.GetSize();i++)
            
                 output<<"["<<i<<"]="<<theArray[i]<<endl;         //这一行编译通不过,此乃第71行
                 return output;
            
}

晨曦的朝阳 发表于 2008-6-1 18:27

二楼和三楼的方法我试了还是不对哦

sunkaidong 发表于 2008-6-1 18:53

#include<iostream>
using namespace std;
namespace  demo{
class Animal
{
      public:
             Animal():itsAge(0){}
             Animal(int Age):itsAge(Age){}
             ~Animal(){}
             int GetAge()const{return itsAge;}
             void 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()
{   


        demo::Array<int> intArray;
        demo::Array<demo::Animal> animalArray;
        demo::intFillArray(intArray);
        demo::animalFillArray(animalArray);
    cout<<"***this is the int array***\n";
    cout<<intArray;
    cout<<"***this is the animal array***\n";
    cout<<animalArray;                      //如果去掉这一行则可以运行...这是第112行
    getchar();
    return 0;
}

晨曦的朝阳 发表于 2008-6-1 20:54

5楼的那样改可以运行了,谢谢啦!!!
还有辛苦2楼和3楼了,嘻嘻!

晨曦的朝阳 发表于 2008-6-1 21:24

再问下5楼,我重新看了你的程序,好像就多加了个命名空间组而已,是吗?还有没有改其它的地方。搞不懂为什么一定要加个命名空间组才行呢

sunkaidong 发表于 2008-6-1 21:44

命名空间有污染。。所以自己用个空间隔开就好了

晨曦的朝阳 发表于 2008-6-2 13:03

哦,这样子啊,谢了!

页: [1]

编程论坛