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

Liczby niewiadomo skąd

Ostatnio zmodyfikowano 2015-03-28 21:12
Autor Wiadomość
kam51
Temat założony przez niniejszego użytkownika
Liczby niewiadomo skąd
» 2015-03-28 18:51:43
Witam, mój problem leży w tym, że używając napisanego przeze mnie kodu:
C/C++
#include <iostream>
#include <cstdio>
using namespace std;
class List;

class element {
private:
    int number;
    element * next;
    friend class List;
    friend ostream & operator <<( ostream &, List & );
   
public:
    element( int data ) {
        number = data;
    }
   
    int getData() {
        return number;
    }
   
};

class List {
private:
    element * head;
    int size;
    friend ostream & operator <<( ostream &, List & );
public:
    List() {
        head = NULL;
        size = 0;
    }
   
    List( const List & lista ) {
        //cout << "wyw" << endl;
        //fflush(stdout);
        if( lista.size == 0 ) {
            size = 0;
            head = NULL;
        }
        else
        {
            size = 0;
            head = NULL;
            element * tmp = lista.head;
            cout << "dupa";
            while( tmp != NULL ) {
                cout << tmp->number << " ";
                fflush( stdout );
                this->addToList( tmp->number );
                tmp = tmp->next;
            }
        }
        cout << "dupa" << endl;
       
    }
   
    ~List() {
        if( head != NULL ) {
           
            cout << "deal";
            element * tmp = head;
            element * temp;
            while( tmp != NULL ) {
                temp = tmp->next;
                delete tmp;
                tmp = temp;
            }
        }
    }
   
    void addToList( int data ) {
        element * newNode = new element( data );
        newNode->next = NULL;
        element * tmp = head;
        if( tmp != NULL ) {
            while( tmp->next != NULL ) {
                tmp = tmp->next;
            }
            tmp->next = newNode;
        }
        else {
            head = newNode;
        }
        size++;
       
    }
   
    List operator +( const List & lista ) {
        List newList;
        element * tmp = this->head;
        while( tmp != NULL ) {
            newList.addToList( tmp->number );
            tmp = tmp->next;
        }
        tmp = lista.head;
        while( tmp != NULL ) {
            newList.addToList( tmp->number );
            tmp = tmp->next;
        }
        cout << newList;
        fflush( stdout );
        return newList;
    }
};


istream & operator >>( istream & str, List & lists ) {
    int data;
    str >> data;
    lists.addToList( data );
    return str;
}

ostream & operator <<( ostream & str, List & lists ) {
    element * tmp = lists.head;
    if( tmp == NULL ) {
        str << "List is empty" << endl;
    }
    else {
        str << "Poczatek" << endl;
        while( tmp != NULL ) {
            str << tmp->number << "--";
            tmp = tmp->next;
        }
        str << "NULL" << endl << "Rozmiar listy: " << lists.size << endl;
    }
    return str;
}


int main() {
    List lista;
    cin >> lista;
    cin >> lista;
    List list2 = lista;
    cout << lista;
    cout << list2;
    List c;
    c = list2 + lista;
    cout << c;
    cout << lista;
    cout << list2;
    return 0;
}

Teoretycznie kod wykonuje to co powinien poza jedną rzeczą: tuż przed zwróceniem obiektu przez operator dodwania obiekt jest ok, ma prawidłowe wartości, problem jest gdy wyświetlam w mainie macierz c, dostaję niewiadomo jakie liczby, takie trochę z niczego, nie wiem dlaczego.
P-129344
pekfos
» 2015-03-28 20:26:57
Brakuje operatora przypisania.
P-129359
kam51
Temat założony przez niniejszego użytkownika
» 2015-03-28 21:08:46
A jeszcze takie pytanie: dlaczego(już po dodaniu operatora przypisania itd) nie mogę zrobić czegoś takiego:
C/C++
cout << list2 + list;
P-129382
pekfos
» 2015-03-28 21:12:01
Bo twój operator<< przyjmuje zwykłą referencję, a nie referencję na stałą.
P-129385
« 1 »
  Strona 1 z 1