PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : 'Template' <-> 'ostream operator <<' aus "C++ in 21 Tagen"



RAHUL
14-04-2009, 14:46
Hallo,

ich habe hier einen Beispielcode aus dem "Jesse Liberty" Buch
"C++ in 21 Tagen" :D
Es geht um Kapitel 19 "Templates"

Der Code wird ohne Fehler kompiliert (Visual C++ 2008 EE),
beim Linken wird jedoch eine sehr kryptische Fehlermeldung zurückgegeben,
die ich nicht versteh.



1>Listing19_04.obj : error LNK2028: Nicht aufgelöstes Token (0A0002B1) ""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)", auf das in Funktion ""int __cdecl main(void)" (?main@@$$HYAHXZ)" verwiesen wird.
1>Listing19_04.obj : error LNK2019: Verweis auf nicht aufgelöstes externes 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)" in Funktion ""int __cdecl main(void)" (?main@@$$HYAHXZ)".
1>C:\Users\Rahul\Documents\Visual Studio 2008\Projects\Jesse Liberty\Listing19_04\Debug\Listing19_04.exe : fatal error LNK1120: 2 nicht aufgelöste externe Verweise.


Vielleicht kann mir jemand erfahrenes helfen.

Vielen Dank
Rahul




#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> // Template und Parameter deklarieren
class Array // die parametrisierte Klasse
{
public:
// Konstruktoren
Array(int itsSize = DefaultSize);
Array(const Array &rhs);
~Array() { delete [] pType; }

// Operatoren
Array& operator=(const Array&);
T& operator[](int offSet) { return pType[offSet]; }
const T& operator[](int offSet) const { return pType[offSet]; }

// Zugriffsfunktionen
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 << "] " << theArray[i] <<endl;
return output;
}

// Implementierungen...

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

// Kopierkonstruktor
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];
}

// =-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++)
pType[i] = rhs[i];
return *this;
}

int main()
{
bool Stop = false; // Flag fuer die Schleife
int offset, value;
Array<int> theArray;

while (!Stop)
{
cout << "Offset (0-9) ";
cout << "und Wert (-1 fuer Abbruch) eingeben: " ;
cin >> offset >> value;

if (offset < 0)
break;

if (offset > 9)
{
cout << "***Bitte Werte zwischen 0 und 9.***\n";
continue;
}

theArray[offset] = value;
}

cout << "\nHier das vollstaendige Array:\n";
cout << theArray << endl;

return 0;
}

BLUESCREEN3D
14-04-2009, 15:13
Hinter

friend ostream& operator<<
fehlt folgendes:

<>

locus vivendi
14-04-2009, 15:22
Die FAQ ist dazu hilfreich:
http://parashift.com/c++-faq-lite/templates.html#faq-35.16

RAHUL
17-04-2009, 14:14
Vielen Dank für die Antworten...:)