Prawdopodobnie zle zwalniana pamiec

0

Witam czy moglby mi ktos powiedziec dlaczego program sie zawiesza ? Udalo mi sie ustalic ze w przeladowaniu operatora przypisania cos zle robie.

#include <iostream>

using namespace std;

class Student {
    string im, naz;
    double oc;

public:
    Student()
        : im("0")
        , naz("0")
        , oc(0)
    {
    }
    Student(string name, string surname, double year)
        : im(name)
        , naz(surname)
        , oc(year)
    {
    }
    string& imie() { return im; }
    const string& imie() const { return im; }
    string& nazwisko() { return naz; }
    const string& nazwisko() const { return naz; }
    double& ocena() { return oc; }
    const double& ocena() const { return oc; }
    friend ostream& operator<<(ostream& out, const Student& ob);
};
ostream& operator<<(ostream& out, const Student& ob)
{
    out << ob.im << " " << ob.naz << " " << ob.oc << endl;
    return out;
}
class Grupa {
    unsigned rozmiar;
    Student* studenci;

public:
    Grupa()
        : rozmiar(0)
        , studenci(new Student){};
    Grupa(const Student* pocz, const Student* kon)
        : rozmiar(kon - pocz)
        , studenci(rozmiar ? new Student[rozmiar] : 0)
    {
        for (unsigned i = 0; i < rozmiar; i++) {
            studenci[i] = pocz[i]; //skad wie zeby tu wszystko przypisac ???
        }
    }
    friend ostream& operator<<(ostream& out, const Grupa& ob);
    const double srednia() const; // dwa consty :D
    Grupa& operator=(const Grupa& ob);
    Student& operator[](unsigned i)
    {
        return studenci[i];
    }
};

Grupa& Grupa::operator=(const Grupa& ob)
{
    if (this != &ob) {
        delete studenci; // Dlaczego nie tablice ?
        Student* studenci = new Student[ob.rozmiar];
        for (unsigned i = 0; i < rozmiar; i++) {
            studenci[i] = ob.studenci[i];
        }
        return *this;
    }
}

const double Grupa::srednia() const
{
    double suma = 0;
    for (unsigned i = 0; i < rozmiar; i++) {
        suma += studenci[i].ocena(); //metoda dostepowa :D
    }
    return suma / rozmiar;
}
ostream& operator<<(ostream& out, const Grupa& ob)
{
    for (unsigned i = 0; i < ob.rozmiar; i++) {
        out << ob.studenci[i];
    }
    return out;
}
int main()
{
    Student bond("James", "Bond", 5.0);
    cout << "Student " << bond.imie() << " " << bond.nazwisko();
    cout << " dostal " << bond.ocena() << endl;
    Student studenci[3] = { Student("Jan", "Kowalski", 3.5),
        Student("Ala", "Nowak", 3.0), Student() };
    const Grupa gr1(studenci, studenci + 3);
    cout << gr1;
    cout << "Srednia ocen tej grupy to " << gr1.srednia() << std::endl;
    Grupa gr2;
    gr2 = gr1;
    gr2[2] = bond;
    cout << gr2;
    //cout << "Srednia ocen tej grupy to " << gr2.srednia() << std::endl
    return 0;
}

0

Debugger?

0

uzywam GNU GCC COMPILER

0

Nie - nie rozumiesz pytania i zgadujesz, myśląc że akurat trafisz.
Pytam w jaki sposób próbowałeś debugować tę aplikację.
Jeśli nie debugowałeś lub nie wiesz co to znaczy to nawet nie odpisuj, tylko sprawdź w internecie i zacznij.

0

Poprawiony kod: http://ideone.com/bUKQz7

 
#include <iostream>
 
using namespace std;
 
class Student {
    string im, naz;
    double oc;
 
public:
    Student()
        : im("0")
        , naz("0")
        , oc(0)
    {
    }
    Student(string name, string surname, double year)
        : im(name)
        , naz(surname)
        , oc(year)
    {
    }
    string& imie() { return im; }
    const string& imie() const { return im; }
    string& nazwisko() { return naz; }
    const string& nazwisko() const { return naz; }
    double& ocena() { return oc; }
    const double& ocena() const { return oc; }
    friend ostream& operator<<(ostream& out, const Student& ob);
};
ostream& operator<<(ostream& out, const Student& ob)
{
    out << ob.im << " " << ob.naz << " " << ob.oc << endl;
    return out;
}
class Grupa {
    unsigned rozmiar;
    Student* studenci;
 
public:
    Grupa()
        : rozmiar(0)
        , studenci(new Student){};
    Grupa(const Student* pocz, const Student* kon)
        : rozmiar(kon - pocz)
        , studenci(rozmiar ? new Student[rozmiar] : 0)
    {
        for (unsigned i = 0; i < rozmiar; i++) {
            studenci[i] = pocz[i]; //skad wie zeby tu wszystko przypisac ???
        }
    }
    friend ostream& operator<<(ostream& out, const Grupa& ob);
    const double srednia() const; // dwa consty :D
    Grupa& operator=(const Grupa& ob);
    Student& operator[](unsigned i)
    {
        return studenci[i];
    }
};
 
Grupa& Grupa::operator=(const Grupa& ob)
{
    if (this != &ob) {
        delete studenci;
        rozmiar = ob.rozmiar;
        studenci = new Student[rozmiar];
        for (unsigned i = 0; i < rozmiar; i++) {
            studenci[i] = ob.studenci[i];
        }
    }
    return *this;
}
 
const double Grupa::srednia() const
{
    double suma = 0;
    for (unsigned i = 0; i < rozmiar; i++) {
        suma += studenci[i].ocena(); //metoda dostepowa :D
    }
    return suma / rozmiar;
}
ostream& operator<<(ostream& out, const Grupa& ob)
{
    for (unsigned i = 0; i < ob.rozmiar; i++) {
        out << ob.studenci[i];
    }
    return out;
}
int main()
{
    Student bond("James", "Bond", 5.0);
    cout << "Student " << bond.imie() << " " << bond.nazwisko();
    cout << " dostal " << bond.ocena() << endl;
    Student studenci[3] = { Student("Jan", "Kowalski", 3.5),
        Student("Ala", "Nowak", 3.0), Student() };
    const Grupa gr1(studenci, studenci + 3);
    cout << gr1;
    cout << "Srednia ocen tej grupy to " << gr1.srednia() << std::endl;
    Grupa gr2;
    gr2 = gr1;
    gr2[2] = bond;
    cout << gr2;
    cout << "Srednia ocen tej grupy to " << gr2.srednia() << std::endl;
    return 0;
}

Konkretniej, to błąd jest tutaj:
Student* studenci = new Student[ob.rozmiar];
oraz w tym, że do nowej grupy nie wpisujesz rozmiaru.
Nasuwa się jedno pytanie, skoro używasz std::string, to czemu nie używasz std::vector?

Aha, no i oczywiście masz memory leaka w klasie Grupa z powodu nie zwalniania studentów.

0

Wszystko działa ale mam pytanko dlaczego nie może być new Student [ob.rozmiar]; do tego co się odwołuje za pomocą tego zapisu jest przecież typu unsigned

0

Dziekuje :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1