Vector problems

I usually try to use my own arrays so I can resize them how I like. I am running into problems with my push_back function though. I think I've used vectors once in my life, so I have no experience really. How do I store this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
vector<intersection> * readagain() {
        vector<intersection> * inter();
        vector<connection> * conn();
        ifstream fin(something);
        if (fin.fail()) {
                cerr << "No File \n";
                exit(1);
        }
        intersection temp1;
        connection temp2;
        while (true) {
                fin >> temp1.lng >> temp1.lat >> temp1.dist >> temp1.state
                    >> temp1.name;
                inter.push_back(temp1);
                if (fin.eof()) break;
        }
        fin.close();
        fin= (something);
        if (fin.fail()) {
                cerr << "No File \n";
                exit(1);
        }
        while (true) {
                fin >> temp2.name >> temp2.type >> temp2.refA >> temp2.refB
                    >> temp2.length;
                conn.push_back(temp2);
                if (fin.eof()) break;
        }
        fin.close();
                
        return inter;
}


the structs I'm using are:
1
2
3
4
5
6
7
8
9
10
11
12
struct connection { 
        string name, type;
        int refA, refB;
        double length;
};
        
struct intersection {
        double lng, lat;
        double dist;
        string state, name;
        vector<connection> conns;
};


Don't know how relevant that is.
Last edited on
http://www.cplusplus.com/forum/articles/40071/#msg218019
vector<intersection> * inter(); is a function declaration.

1
2
3
4
5
6
7
8
9
10
/*
        while (true) {
                fin >> temp1.lng >> temp1.lat >> temp1.dist >> temp1.state
                    >> temp1.name;
                inter.push_back(temp1);
                if (fin.eof()) break;
        }
*/
while (fin >> temp1.lng >> temp1.lat >> temp1.dist >> temp1.state >> temp1.name)
   inter.push_back(temp1);
Thanks for your help. I've modified it and this is what I have so far for my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
vector<intersection> * readagain() {
        vector<intersection> * inter;
        vector<connection> * conn;
        ifstream fin (something);
        if (fin.fail()) {
                cerr << "No File \n";
                exit(1);
        }
        intersection temp1;
        connection temp2;
        while (true) {
                fin >> temp1.lng >> temp1.lat >> temp1.dist >> temp1.state;
                getline(fin, temp1.name);
                inter->push_back(temp1);
                if (fin.eof()) break;
        }
        fin.close();
        ifstream few (something);
        if (few.fail()) {
                cerr << "No File \n";
                exit(1);
        }
        while (few >> temp2.name >> temp2.type >> temp2.refA >> temp2.refB >> temp2.length) {
                conn->push_back(temp2);
                if (few.eof()) break;
        }
        few.close();
        int i=0;
        while (i<conn->size()) {
                inter->at(conn->at(i).refA).conns->push_back(conn->at(i));
                inter->at(conn->at(i).refB).conns->push_back(conn->at(i));
                i++;
        }
        return inter;
}


It stops working at the second loop though. The first one works fine and I am able to print the vector that I get, but I don't know why the second one doesn't work.
Last edited on
I should also mention that the loop is capable of getting the first line, but nothing after that.
You are trying to use uninitialized pointers.
There is no good reason to use pointers there.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<intersection> readagain() {
        vector<intersection> inter;
        vector<connection> conn;
//...
        while (fin >> temp1.lng >> temp1.lat >> temp1.dist >> temp1.state >> temp1.name){
                inter.push_back(temp1);
                //no need for a useless eof() check
        }
//...
        while (few >> temp2.name >> temp2.type >> temp2.refA >> temp2.refB >> temp2.length) {
                conn.push_back(temp2);
        }
        return inter;
}
Last edited on
Topic archived. No new replies allowed.