Function not changing contents of Vector

Jul 31, 2011 at 12:36am
Is it because I need a pointer to it?
Jul 31, 2011 at 12:40am
Probably.

If you are passing by value, then you are actually changing a copy of the vector, and not the original vector itself.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void by_ref(vector<int>& foo) // <- note the '&'
{
  foo.push_back(0);  // actually changes main_foo
}


void by_val(vector<int> foo) // <- note, no '&'
{
  foo.push_back(0);  // does not change main_foo
}

int main()
{
  vector<int> mainfoo;
  by_ref(mainfoo);  // change mainfoo;
  by_val(mainfoo);  // does not change mainfoo
}
Jul 31, 2011 at 12:42am
I tried putting the ampersand in front as above, but then the program crashes
Jul 31, 2011 at 12:48am
then you're doing something wrong.

post some code and I'll see if I can spot the problem.
Jul 31, 2011 at 12:51am
Ok, Its just too big to post all of it, so Ill post the Functions then the main

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstdio>
#include <vector>
#include <time.h>
#include <string>
#include <cctype>

using namespace std;

typedef pair<string,string> Pair_allele;

struct Person {
    int id;
    int generation;
    int life;
    bool alive;
    vector<Pair_allele> Genotypes;
    vector<string> Phenotypes;
    vector<string> Gene_List;
    Person(int i, int g) {
        id = i;
        generation = g;
        life = 3;
        alive = true;
    }
};

void clear_screen() {
    system("CLS");
    cout << "*============================*" << endl;
    cout << "|       Life Simulator       |" << endl;
    cout << "*============================*" << endl;
    cout << endl << "*=========================================================*" << endl << endl;
}

void Breed( vector<Person> list, vector<Pair_allele> Phenotypes, vector<Pair_allele> Genotypes, int g, vector<string> genes) {
    int size = list.size();
    for (int i = 0; i < size/2; i++) {
        int person_one = i;
        int person_two = rand() % (list.size()/2) + (list.size()/2);
        vector<Pair_allele> new_genotypes;
        vector<string> new_phenotypes;
        for (int j = 0; j < genes.size(); j++) {
            pair<string,string> Poss_one = (Pair_allele(list[person_one].Genotypes[j].first, list[person_two].Genotypes[j].first));
            pair<string,string> Poss_two = (Pair_allele(list[person_one].Genotypes[j].first, list[person_two].Genotypes[j].second));
            pair<string,string> Poss_three = (Pair_allele(list[person_one].Genotypes[j].second, list[person_two].Genotypes[j].second));
            int poss = rand() % 3;
            if (poss == 0) {
                new_genotypes.push_back(Pair_allele(Poss_one.first, Poss_one.second));
            }
            else if (poss == 1) {
                new_genotypes.push_back(Pair_allele(Poss_two.first, Poss_two.second));
            }
            else {
                new_genotypes.push_back(Pair_allele(Poss_three.first, Poss_three.second));
            }
            char c = new_genotypes[j].first[0];
            char d = new_genotypes[j].second[0];
            if (isupper(c)|| isupper(d) == true) {
                new_phenotypes.push_back(Phenotypes[j].first);
            }
            else {
                new_phenotypes.push_back(Phenotypes[j].second);
            }
        }
        list.push_back(Person(list.size()+1, g));
        for (int q = 0; q < genes.size(); q++) {
            list[list.size()-1].Genotypes.push_back( Pair_allele(new_genotypes[q].first, new_genotypes[q].second) );
            list[list.size()-1].Phenotypes.push_back( new_phenotypes[q] );
        }

    }
}
void Print_OverView(vector<Person> &P_List, vector<string> Genes, vector<Pair_allele> Pheno, vector<Pair_allele> Genotypes, int g) {
    vector<Pair_allele> permlist;
    for (int i = 0; i < Genes.size(); i++) {
        permlist.push_back(Pair_allele(Genotypes[i].first, Genotypes[i].first));
        permlist.push_back(Pair_allele(Genotypes[i].first, Genotypes[i].second));
        permlist.push_back(Pair_allele(Genotypes[i].second, Genotypes[i].second));
    }
    vector<int> numperms;
    for (int i = 0; i < permlist.size(); i++) {
        int tempa = 0;
        numperms.push_back(tempa);
    }
    vector<pair<int,int> > numpheno;
    for (int i = 0; i < Pheno.size(); i++) {
        pair<int,int> temppair;
        numpheno.push_back(temppair);
        numpheno[i].first = 0;
        numpheno[i].second = 0;
    }
    for (int i = 0; i < P_List.size(); i++) {
        for (int j = 0; j < Genes.size(); j++) {
            if (P_List[i].Gene_List[j] == Genes[j]) {
                if (P_List[i].Phenotypes[j] == Pheno[j].first) {
                    numpheno[j].first++;
                }
                else {
                    numpheno[j].second++;
                }
                if (P_List[i].Genotypes[j].first == permlist[j*3].first && P_List[i].Genotypes[j].second == permlist[j*3].second) {
                    numperms[j*3]++;
                }
                else if (P_List[i].Genotypes[j].first == permlist[j*3+1].first && P_List[i].Genotypes[j].second == permlist[j*3+1].second) {
                    numperms[j*3+1]++;
                }
                else if (P_List[i].Genotypes[j].first == permlist[j*3+2].first && P_List[i].Genotypes[j].second == permlist[j*3+2].second) {
                    numperms[j*3+2]++;
                }
            }
        }

    }
    system("CLS");
    cout << "*============================*" << endl;
    cout << "|       Life Simulator       |" << endl;
    cout << "*============================*" << endl;
    cout << endl << "*=========================================================*" << endl << endl;
    cout << "Generation " << g << endl;
    cout << "Population = " << P_List.size() << endl << endl;
    for (int i = 0; i < Genes.size(); i++) {
        cout << "Information About Gene " << Genes[i] << endl;
        cout << "   Number Of " << Pheno[i].first << " " << Genes[i] << " = " << numpheno[i].first << endl;
        cout << "   Number Of " << Pheno[i].second << " " << Genes[i] << " = " << numpheno[i].second << endl;
        cout << "   Number Of Allele Pair " << permlist[i*3].first << permlist[i*3].second << " = " << numperms[i*3] <<endl;
        cout << "   Number Of Allele Pair " << permlist[i*3+1].first << permlist[i*3+1].second << " = " << numperms[i*3+1] <<endl;
        cout << "   Number Of Allele Pair " << permlist[i*3+2].first << permlist[i*3+2].second << " = " << numperms[i*3+2] <<endl << endl;
    }
}


EDIT: Sorry, couldn't find any spoiler tags
Last edited on Jul 31, 2011 at 12:55am
Jul 31, 2011 at 1:28am
If the problem is in Breed() or Print_Overview(), then do as Disch suggested. If it generates a compiler error, post it.
Jul 31, 2011 at 1:33am
If I put an ampersand as Disch said in the breed function, I dont get errors but when the program runs it crashes, same if I put it in the Breed and print overview function, if I just [put it in Print Overview, then it runs the same as without
Jul 31, 2011 at 2:15am
what line does it crash on?
Jul 31, 2011 at 2:18am
not sure, Id imagine its in the breed function though
Jul 31, 2011 at 2:39am
Use a debugger and figure out where it's crashing. You might just find the error when doing that.
Jul 31, 2011 at 2:44am
Right, the debugger found the error in Print OverView, in this line
if (P_List[i].Gene_List[j] == Genes[j]) {
Jul 31, 2011 at 2:46am
Ok, I removed that line and its working now, must have been really tired to put that in for no reason
Topic archived. No new replies allowed.