Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

szablony, a funkcje zaprzyjaźnione (template, friend)

Ostatnio zmodyfikowano 2009-11-02 15:38
Autor Wiadomość
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:

C/C++
#include <iostream>

using namespace std;

template < class T >

class stos;

template < class T >

class element
{
    T value; //wartosc
    class element * next; //wskaźnik na następny element
    friend class stos < T >;
    element()
    {
        next = NULL;
    };
    ~element() { };
   
   
};

template < class T >

class stos
{
    class element < T > * first; //wskaźnik na pierwszy element
    class element < T > * tab; //dynamiczna tablica
    unsigned int number; //liczba elementow
   
public:
   
    stos()
    {
        first = NULL;
        number = 0;
        tab = new element < T >[ number ];
       
    };
   
   
   
   
    ~stos()
    {
        delete[] tab;
       
    };
   
   
    friend istream & operator >> <>( istream & in, stos < T > & n ); //dodajemy element do stosu
    friend ostream & operator << <>( ostream & out, const stos < T > & m ); //wypisujemy stos na ekranie
    stos < T > operator -( int ile ); //zdejmujemy określoną ilość elementów ze stosu
};


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"
P-11231
DejaVu
» 2009-11-02 15:27:11
Ten kod wydaje mi się jakiś znajomy...
P-11233
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 :> ?
P-11234
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:
C/C++
friend istream & operator >> <>( istream & in, stos < T > & n ); //dodajemy element do stosu
friend ostream & operator << <>( ostream & out, const stos < T > & m ); //wypisujemy stos na ekranie
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:
C/C++
#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 );
}; //class TSzablon

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:
C/C++
#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 );
}; //class TSzablon

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;
};
P-11235
« 1 »
  Strona 1 z 1