level 1
羊云
楼主
#include<iostream>
using namespace std;
const int defaultSize=10;
class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const {return itsWeight;}
void Display() const {cout<<itsWeight;}
private:
int itsWeight;
};
Animal::Animal(int weight):
itsWeight(weight)
{}
Animal::Animal():
itsWeight(0)
{}
template<class T>
class Array
{
public:
Array(int size=defaultSize);
Array(const Array&);
~Array() {delete [] pType;}
Array &operator=(const Array&);
T& operator[](int offset) {return pType[offset];}
const T& operator[](int offset) const {return pType[offset];}
int GetSize() const {return itsSize;}
template<class T>
friend ostream & operator<<(ostream&,Array<T>&);
private:
T *pType;
int itsSize;
};
template<class T>
ostream & operator<<(ostream &output,Array<T> &rhs)
{
for(int i=0;i<rhs.itsSize;i++)
output<<"["<<i<<"]:"<<rhs[i]<<endl;
return output;
}
template<class T>
Array<T>::Array(int size):
itsSize(size)
{pType=new T[size];}
template<class T>
Array<T>::Array(const Array &rhs)
{
itsSize=rhs.GetSize();
pType=new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i]=rhs[i];
}
template<class T>
Array<T> & Array<T>::operator =(const Array &rhs)
{
if(this==&rhs)
return *this;
delete [] pType;
itsSize=rhs.GetSize();
pType=new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i]=rhs[i]l;
return *this;
}
int main()
{
bool stop=false;
int offset,value;
Array<int> theArray;
while(stop==false)
{
cout<<"Enter an offset(0--9) and an value(-1 to stop): ";
cin>>offset>>value;
if(offset<0)
break;
if(offset>9)
{
cout<<"Please enter an offset from 0 to 9: ";
continue;
}
theArray[offset]=value;
}
cout<<"\nHere is the entire array:\n";
cout<<theArray<<endl;
return 0;
}
报错如下:
--------------------Configuration: project81 - Win32 Debug--------------------
Compiling...
FriendtemplateFunc.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\project81\FriendtemplateFunc.cpp(105) : error C2563: mismatch in formal parameter list
D:\Program Files\Microsoft Visual Studio\MyProjects\project81\FriendtemplateFunc.cpp(105) : error C2568: '<<' : unable to resolve function overload
could be 'class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &__cdecl std::endl(class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &)'
d:\program files\microsoft visual studio\vc98\include\ostream(377) : see declaration of 'endl'
or 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)'
d:\program files\microsoft visual studio\vc98\include\ostream(372) : see declaration of 'endl'
or 'class std::basic_ostream<_E,_Tr> &__cdecl std::endl(class std::basic_ostream<_E,_Tr> &)'
d:\program files\microsoft visual studio\vc98\include\ostream(367) : see declaration of 'endl'
执行 cl.exe 时出错.
FriendtemplateFunc.obj - 1 error(s), 0 warning(s)
2013年12月16日 10点12分
1
using namespace std;
const int defaultSize=10;
class Animal
{
public:
Animal(int);
Animal();
~Animal() {}
int GetWeight() const {return itsWeight;}
void Display() const {cout<<itsWeight;}
private:
int itsWeight;
};
Animal::Animal(int weight):
itsWeight(weight)
{}
Animal::Animal():
itsWeight(0)
{}
template<class T>
class Array
{
public:
Array(int size=defaultSize);
Array(const Array&);
~Array() {delete [] pType;}
Array &operator=(const Array&);
T& operator[](int offset) {return pType[offset];}
const T& operator[](int offset) const {return pType[offset];}
int GetSize() const {return itsSize;}
template<class T>
friend ostream & operator<<(ostream&,Array<T>&);
private:
T *pType;
int itsSize;
};
template<class T>
ostream & operator<<(ostream &output,Array<T> &rhs)
{
for(int i=0;i<rhs.itsSize;i++)
output<<"["<<i<<"]:"<<rhs[i]<<endl;
return output;
}
template<class T>
Array<T>::Array(int size):
itsSize(size)
{pType=new T[size];}
template<class T>
Array<T>::Array(const Array &rhs)
{
itsSize=rhs.GetSize();
pType=new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i]=rhs[i];
}
template<class T>
Array<T> & Array<T>::operator =(const Array &rhs)
{
if(this==&rhs)
return *this;
delete [] pType;
itsSize=rhs.GetSize();
pType=new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i]=rhs[i]l;
return *this;
}
int main()
{
bool stop=false;
int offset,value;
Array<int> theArray;
while(stop==false)
{
cout<<"Enter an offset(0--9) and an value(-1 to stop): ";
cin>>offset>>value;
if(offset<0)
break;
if(offset>9)
{
cout<<"Please enter an offset from 0 to 9: ";
continue;
}
theArray[offset]=value;
}
cout<<"\nHere is the entire array:\n";
cout<<theArray<<endl;
return 0;
}
报错如下:
--------------------Configuration: project81 - Win32 Debug--------------------
Compiling...
FriendtemplateFunc.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\project81\FriendtemplateFunc.cpp(105) : error C2563: mismatch in formal parameter list
D:\Program Files\Microsoft Visual Studio\MyProjects\project81\FriendtemplateFunc.cpp(105) : error C2568: '<<' : unable to resolve function overload
could be 'class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &__cdecl std::endl(class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &)'
d:\program files\microsoft visual studio\vc98\include\ostream(377) : see declaration of 'endl'
or 'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)'
d:\program files\microsoft visual studio\vc98\include\ostream(372) : see declaration of 'endl'
or 'class std::basic_ostream<_E,_Tr> &__cdecl std::endl(class std::basic_ostream<_E,_Tr> &)'
d:\program files\microsoft visual studio\vc98\include\ostream(367) : see declaration of 'endl'
执行 cl.exe 时出错.
FriendtemplateFunc.obj - 1 error(s), 0 warning(s)