标题:简单模板应用出错(帮帮忙)
只看楼主
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
 问题点数:0 回复次数:8 
简单模板应用出错(帮帮忙)
哪位朋友帮忙看下程序,用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
zhengqiang_hust
该用户已被删除
得分:0 
应该是这样
提示: 作者被禁止或删除 内容自动屏蔽
2008-06-01 13:45
中学者
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:20
帖 子:3554
专家分:80
注 册:2007-9-14
得分:0 
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-06-01 14:24
晨曦的朝阳
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2008-1-24
得分:0 
二楼和三楼的方法我试了还是不对哦
2008-06-01 18:27
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
#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-06-01 18:53
晨曦的朝阳
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
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
得分:0 
命名空间有污染。。所以自己用个空间隔开就好了

学习需要安静。。海盗要重新来过。。
2008-06-01 21:44
晨曦的朝阳
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.418142 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved