samedi 29 avril 2017

Reading in from file with modern c++ - data is not stored

maybe I get something wrong with shared_pointers or there is some basic shortcoming of mine but I couldn't get this right. So I want to read in some data from a file. There are position and momentum data on each line of the data file and the first line stores the number of data points.

I need to read this in to my data structure and for some reason my graph would not fill, although the data reads in correctly.

const int dim = 3; // dimension of problem

template <typename T, typename G>
// T is the type of the inputted locations and G is the type of the 
// distance between them
// for example: int point with float/double distance
struct Node{

    std::pair< std::array<T, dim>,std::pair< std::array<T, dim>, G > > pos; // position
    std::pair< std::array<T, dim>,std::pair< std::array<T, dim>, G > > mom; // momentum
    // a pair indexed by a position in space and has a pair of position
    // and the distance between these points

};

template <typename T, typename G>
struct Graph{

    int numOfNodes;
    std::vector< Node<T,G> > nodes;

};

This is the data structure and here's my read function (std::cout-s are only for testing):

template <typename T, typename G>
std::istream& operator>>(std::istream& is, std::shared_ptr< Graph<T,G> >& graph){

    is >> graph->numOfNodes; // there's the number of nodes on the first line of the data file
    std::cout << graph->numOfNodes << "\n";

    for(int k=0; k<graph->numOfNodes; k++){

        Node<T,G> temp;

        for(auto i : temp.pos.first){
            is >> i;
            std::cout << i << "\t";
        }

        std::cout << "\t";

        for(auto i : temp.mom.first){
            is >> i;
            std::cout << i << "\t";
        }

        std::cout << "\n";

        graph->nodes.push_back(temp);

    }

    return is;

 }

I have an output function as well. So if I output the graph which I intended to fill during read-in is zeroed out. Number of nodes os correct however positions and momente are all zeroed out. What did I do wrong? Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire