大家帮我看看这个程序。拜托!
#include <iostream>using namespace std;
class Animal
{
private:
int itsWeight;
public:
Animal();
Animal(int);
~Animal();
int GetWeight() const
{
return itsWeight;
}
void Display() const
{
cout << itsWeight;
}
};
Animal::Animal():
itsWeight(1)
{}
Animal::Animal(int weight):
itsWeight(weight)
{}
Animal::~Animal() {}
template <class T>
class Array
{
private:
int itsSize;
T * pType;
public:
Array(int);
Array(const Array &);
~Array();
int GetSize() const
{
return itsSize;
}
T * GetType() const
{
return pType;
}
Array & operator =(const Array &) const;
T & operator [](int);
const T & operator [](int) const;
friend ostream & operator <<(ostream &,Array<T> &);
};
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
pType = new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i] = 0;
}
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()
{
delete [] pType;
pType = 0;
itsSize = 0;
}
template <class T>
Array<T> & Array<T>::operator =(const Array & rhs) const
{
if(this == &rhs)
return * this;
delete [] pType;
pType = 0;
itsSize = rhs.GetSize();
pType = new T[itsSize];
for(int i=0;i<itsSize;i++)
pType[i] = rhs[i];
}
template <class T>
T & Array<T>::operator [](int offset)
{
if(offset > itsSize)
return pType[itsSize-1];
else
return pType[offset];
}
template <class T>
const T & Array<T>::operator [](int offset) const
{
if(offset > itsSize)
return pType[itsSize-1];
else
return pType[offset];
}
template <class T>
ostream & operator <<(ostream & output,Array<T> & theArray)
{
for(int i=0;i<theArray.GetSize();i++)
{
output << theArray[i];
}
return output;
}
int main()
{
Array<int> theArray(10);
int offset,value;
bool Stop = false;
while(!Stop)
{
cout << "Please input the offset(0-9) and the value:";
cin >> offset >> value;
if(offset < 0)
break;
if(offset > 9)
{
cout << "The offset must be in 0 to 9!" << endl;
continue;
}
theArray[offset] = value;
}
cout << "Here is theArray:" << endl;
cout << theArray << endl;
return 0;
}
我是用vs2008编译的,编译能通过,但是运行却出现了错误。
错误 1 error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Array<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Array@H@@@Z),该符号在函数 _main 中被引用 ex19-4.obj Example 19
错误 2 fatal error LNK1120: 1 个无法解析的外部命令 F:\Program Files\source\Example 19\Debug\Example 19.exe Example 19