szablony, a funkcje zaprzyjaźnione (template, friend)
Ostatnio zmodyfikowano 2009-11-02 15:38
Riddle Temat założony przez niniejszego użytkownika |
szablony, a funkcje zaprzyjaźnione (template, friend) » 2009-11-02 15:13:33 Witam, mam do napisania stos z użyciem szablonu. Wydaje się że jestem blisko końca ale nie mogę sobie poradzić z pewnym błędem: oto kod: #include <iostream>
using namespace std;
template < class T >
class stos;
template < class T >
class element { T value; class element * next; friend class stos < T >; element() { next = NULL; }; ~element() { }; };
template < class T >
class stos { class element < T > * first; class element < T > * tab; unsigned int number; public: stos() { first = NULL; number = 0; tab = new element < T >[ number ]; }; ~stos() { delete[] tab; }; friend istream & operator >> <>( istream & in, stos < T > & n ); friend ostream & operator << <>( ostream & out, const stos < T > & m ); stos < T > operator -( int ile ); };
template < class T >
istream & operator >>( istream & in, stos < T > & n ) { T wartosc; in >> n.element < T >.wartosc; n.number++; if( n->first == NULL ) { n.tab = new element < T >[ n.number - 1 ]; n->first = n.tab[ n.number - 1 ]; n.tab[ n.number - 1 ]->next = NULL; n.value = wartosc; } else { n.tab[ n.number ].value = wartosc; n.tab[ n.number ]->next = n.tab[ n.number - 1 ]; }; return in; };
template < class T >
ostream & operator <<( ostream & out, const stos < T > & m ) { int i; for( i = 0; i <= m.number; i++ ) { out << "Element numer " << i << "\t" << m.element.value << endl; }; return out; };
template < class T > stos < T > stos < T >::operator -( int ile ) { number = number - ile; if( number > 1 ) { tab = new element < T >[ number ]; tab->next = tab[ number - 1 ].value; } else { delete[] tab; }; return * this; };
int main( void ) { cout << "Testujemy stos!" << endl; stos < int > obiekt; int i; for( i = 0; i <= 10; i++ ) { cout << "Wprowadz " << i << " element\n"; cin >> obiekt; cout << endl; }; cout << obiekt; obiekt = obiekt - 1; cout << obiekt; system( "PAUSE" ); return 0; };
Kompilator (dev) w linijce tworzenia obiektu mówi mniej więcej: "In instantiation of stos'<int>' instantianted here" |
|
DejaVu |
» 2009-11-02 15:27:11 Ten kod wydaje mi się jakiś znajomy... |
|
Riddle Temat założony przez niniejszego użytkownika |
» 2009-11-02 15:32:22 Znajomy ? Sam pisałem całość, skoro znajomy to może znasz źródło błędu :> ? |
|
DejaVu |
» 2009-11-02 15:38:54 W kodzie jest pełno błędów... jeden popędza drugi. Nie kompilowałeś tego co kilka nowych wierszy? Ustaw na początek selektory dostępu w klasach na public dla wszystkich zmiennych i wykomentuj wiersze: friend istream & operator >> <>( istream & in, stos < T > & n ); friend ostream & operator << <>( ostream & out, const stos < T > & m );
Jak ponaprawiasz kod logicznie to wklej go ponownie - zobaczę wtedy co trzeba zrobić żeby nie musiały być selektory publiczne, tylko friend zadziałał prawidłowo. /edit: A kod wydawał się znajomy przez te linijki, które napisałem Ci żebyś wywalił :) http://forum.ddt.pl/?Tid=856/edit2: Pod Visualem powinno zadziałać coś takiego: #include <iostream>
template < class T > class TSzablon { protected: T m_tDane; public: TSzablon( const T & tDane ); template < class T > friend std::ostream & operator <<( std::ostream & out, const TSzablon < T > & m ); };
template < class T > TSzablon < T >::TSzablon( const T & tDane ) : m_tDane( tDane ) { }
template < class T > std::ostream & operator <<( std::ostream & out, const TSzablon < T > & m ) { out << m.m_tDane; return out; }
int main( void ) { TSzablon < int > ble( 123 ); std::cout << "Ble = " << ble << std::endl; return 0; };
Pod mingw'a... muszę poszukać :P /edit3: #include <iostream>
template < class T > class TSzablon { protected: T m_tDane; public: TSzablon( const T & tDane ); template < class TT > friend std::ostream & operator <<( std::ostream & out, const TSzablon < TT > & m ); };
template < class T > TSzablon < T >::TSzablon( const T & tDane ) : m_tDane( tDane ) { }
template < class T > std::ostream & operator <<( std::ostream & out, const TSzablon < T > & m ) { out << m.m_tDane; return out; }
int main( void ) { TSzablon < int > ble( 123 ); std::cout << "Ble = " << ble << std::endl; return 0; }; |
|
« 1 » |