lundi 28 août 2017

C++ can't push class objects into std::vector

I thought i would be used to the usage of a vector. But somehow im getting problems while pushing class Objects into my std::vector.

My vector is declared in my Class : "Galaxy":

//Galaxy.h

#ifndef GALAXIS_H
#define GALAXIS_H
#include "Stern.h"
#include <vector>
class Galaxis {
public:
    Galaxis();
    Galaxis(const string& datei);
    virtual ~Galaxis();
    void print()const;
private:
    vector<Stern> galaxy;

};

#endif /* GALAXIS_H */

And now i want to use it to hold my "Star" objects. In the constructor

//Galaxy.cpp

Galaxis::Galaxis(const string& datei) {
    ifstream ifs;
    ifs.open(datei, ios::in);
    if (ifs.is_open()) {
        Stern star;

        while (!ifs.eof()) {
            ifs>>star;
            this->galaxy.push_back(star);

        }
        this->print();
        ifs.close();
    } else {
        throw invalid_argument("File '" + datei + "' not Found");
    }
}

Now i want to print them with my print() method,

void Galaxis::print() const {
    for (int i=0; i < galaxy.size(); i++) {
        galaxy[i].print();
    }
}

Somehow the output only shows empty "Stern" objects. The streaming operator has been correctly implemented and tested, aswell as the print method for the "Stern" objects. Hope you can help me

Aucun commentaire:

Enregistrer un commentaire