K
Kuvaldis
Помогите, так как уже вырвал на себе последние волосы. Проблема в шаблоне ниже с перегрузкой оператора вывода. Проверил, вроде бы все правильно. Но: 6 visual компилирует без проблем, в то время как builder 6 и visual 8 не хотят: выдается ошибка трансляции.
Код:
//---------------------------------------------------------------------------
#include <iostream>
using namespace std;
//---------------------------------------------------------------------------
const int DefaultSize = 10;
//---------------------------------------------------------------------------
class Animal
{
protected:
int itsWeight;
public:
// constructors
Animal() : itsWeight(0) {}
Animal(int weight) : itsWeight(weight) {}
~Animal() {}
// access methods
int GetWeight() const { return itsWeight; }
void Display() const { cout << itsWeight << endl; }
};
//---------------------------------------------------------------------------
template <class T>
class Array
{
private:
T* pType;
int itsSize;
public:
// constructors
Array(int size = DefaultSize); // параметр по умолчанию
Array(const Array & rhs);
~Array() { delete [] pType; }
// operators
Array & operator = (const Array & theArray);
T & operator [] (int offset) { return pType[offset]; }
const T & operator [] (int offset) const { return pType[offset]; } // для конструктора копий
// access methods
int GetSize() const { return itsSize; }
friend ostream & operator << (ostream & output, Array<T> & );
};
//---------------------------------------------------------------------------
template <class T>
ostream & operator << (ostream & output, Array<T> & theArray);
//---------------------------------------------------------------------------
int main(int argc, char* argv[])
{
int i, n;
Array <int> theArray;
n = theArray.GetSize();
for(i = 0; i < n; i++)
theArray[i] = i * 2;
cout << theArray;
cin >> i;
return 0;
}
//---------------------------------------------------------------------------
template <class T>
Array<T>::Array(int size) : itsSize(size)
{
pType = new T[size];
for(int i = 0; i < size; i++)
pType[i] = 0;
}
//---------------------------------------------------------------------------
template <class T>
Array<T>::Array(const Array & rhs)
{
itsSize = rhs.itsSize;
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 & theArray)
{
if(this == &theArray)
return *this;
itsSize = rhs.itsSize;
delete [] pType;
pType = new T[itsSize];
for(int i = 0; i < itsSize; i++)
pType[i] = theArray[i];
return *this;
}
//---------------------------------------------------------------------------
template <class T>
ostream & operator << (ostream & output, Array<T> & theArray)
{
for(int i = 0; i < theArray.GetSize(); i++)
output << "Array[" << i << "] = " << theArray[i] << endl;
return output;
}
//---------------------------------------------------------------------------