Templates Error

I using visual C++ 2003 as a IDE and I'm trying to work through Jesse Liberty's Learn C++ in 21 days I have reached chapter 19 without problems.

But I can't seem to create a friend operator that overloads the << operator

I keep getting the error:

error LNK2001: unresolved external symbol "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@$$FYAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Array@H@@@Z)

Can anyone tell me how I can compile fix the error
Below is the source code:

//Listing 19.4 Using Operator ostream
#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> // declare the template and the parameter
class Array // the class being parameterized
{
public:
// constructors
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType; }

// operators
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const
{ return pType[offSet]; }
// accessors
int GetSize() const { return itsSize; }

friend ostream& operator<< (ostream&, Array<T>&);

private:
T *pType;
int itsSize;
};

template <class T>
ostream& operator<< (ostream& output, Array<T>& theArray)
{
for (int i = 0; i<theArray.GetSize(); i++)
output << "[" << i << "] " << theArrayIdea << endl;
return output;
}

// implementations follow...

// implement the Constructor
template <class T>
Array<T>::Array(int size):
itsSize(size)
{
pType = new T[size];
for (int i = 0; i<size; i++)
pTypeIdea = 0;
}

// copy constructor
template <class T>
Array<T>::Array(const Array &rhs)
{
itsSize = rhs.GetSize();
pType = new T[itsSize];
for (int i = 0; i<itsSize; i++)
pTypeIdea = rhsIdea;
}

// operator=
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++)
pTypeIdea = rhsIdea;
return *this;
}

int main()
{
bool Stop = false; // flag for looping
int offset, value;
Array<int> theArray;

while (!Stop)
{
cout << "Enter an offset (0-9) ";
cout << "and a value. (-1 to stop): " ;
cin >> offset >> value;

if (offset < 0)
break;

if (offset > 9)
{
cout << "***Please use values between 0 and 9.***\n";
continue;
}

theArray[offset] = value;
}

cout << "\nHere's the entire array:\n";
cout << theArray << endl;
return 0;
}

[3879 byte] By [Lex-Man] at [2007-12-22]
# 1

Hi the problem is with this code:

template<typename T>
class Array {
// ...
friend ostream& operator<<(ostream&, Array<T>&);
};

template<typename T>
ostream& operator<<(ostream& output, Array<T>& theArray)
{
// ...
}

When this class template Array<T> is specialized with the type int is creates the following 'friend' declaration:

friend ostream& operator<<(ostream&, Array<T>&);

In otherwords is states that the operator with this signature in the nearest enclosing namespace is a friend of this class. Note: this is a signature of a global operator - not a global operator template or a specialization of a global operator template - hence it does not match the delcaration of the global operator template that is in your code. You should change your code to the following:

template<typename T>
class Array;

template<typename T>
ostream& operator<<(ostream& output, Array<T>& theArray);

template<typename T>
class Array {
// ...
template<typename U>
friend ostream& operator<<(ostream&, Array<U>&);
};

template<typename T>
ostream& operator<<(ostream& output, Array<T>& theArray)
{
// ...
}

Now the declaration of friend function matches the declaration of the enclosing operator template

JonathanCaves-MSFT at 2007-8-30 > top of Msdn Tech,Visual C++,Visual C++ Language...