1

have struct_gene and struct_units. i read all Struck_gene values to vector. How do i assign pointer for struck_units.has_gene[i] to point to some value in struct_genevector so i dont have to write full copy of stuckt_gene And after that read it.

I tried for some time but didn't manage to get working solution

http://a.imageshack.us/img195/8607/001tfa.jpg

Edit - Here's the code as retrieved by Jakobud:

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    vector<struct_gene> *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list;
    struct_gene * t_struct_gene;
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene;
    //string name, description;

    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

//   for (int i =0; i<vector_gene.size(); i++) {
//      cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
//   }
    struct_unit *t_struct_unit;
    t_struct_unit = new struct_unit;

    srand ( time(NULL) );

    vector<struct_unit> vector_units;

    for (int i=0; i<default_unit_count; i++) {

        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<0; j++) {
            cout <<vector_units[i].has_genes[j].name;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;
}
2
  • 3
    Dont post your code as an image. Now no one can use it :( Commented Jul 14, 2010 at 1:31
  • 3
    Please paste your code into the question. There's a button that looks like "101 010" to format it as code. Commented Jul 14, 2010 at 1:31

4 Answers 4

1

t_struct_unit->has_genes[i] = &vector_gene

This will assign a pointer to the your gene list into the unit's ith slot.

But beware, since vector_gene is not a global variable, it (and all its contents) will be filled with garbage some time after the function it is in returns. Since it's in main, that's probably a non-issue for you, but you should know about the difference between stack and heap allocations.

Also, are you sure you want each struct_unit to hold a whole array of vectors of struct_genes? Don't you want each one to hold just twelve genes, rather than 12 vectors of genes?

Sign up to request clarification or add additional context in comments.

3 Comments

Right now it is in main because i am checking out how this stuff works. For later i plan to leave all vectors in main () and do operations to them thought functions
tried this "t_struct_unit->has_genes[i] = &vector_gene[i]" and it didnt work. t_struct_unit->has_genes[i] = &vector_gene[rand()%vector_gene.size()];
Sorry, you have only one gene vector. My mistake. Edited.
0

Try this;

1) instance struct_unit

struct_unit aunit;

2) Then inside the loop set the gene in the desired position (SOME_INDEX to be defined by you)

//inside loop
vector_gene.push_back(*t_struct_gene);
aunit.has_genes[SOME_INDEX] = *t_struct_gene;
//

1 Comment

Tried doing it this way to and it didn't work. t_struct_unit->has_genes[i] = *vector_gene[rand()%vector_gene.size()]
0

Here is his code:

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    vector<struct_gene> *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list;
    struct_gene * t_struct_gene;
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene;
    //string name, description;

    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

//   for (int i =0; i<vector_gene.size(); i++) {
//      cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
//   }
    struct_unit *t_struct_unit;
    t_struct_unit = new struct_unit;

    srand ( time(NULL) );

    vector<struct_unit> vector_units;

    for (int i=0; i<default_unit_count; i++) {

        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<0; j++) {
            cout <<vector_units[i].has_genes[j].name;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;
}

Comments

0

http://codepad.org/TZpNS59a:

#include <iostream>
#include <fstream>
#include <vector>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
struct struct_gene {
    string name;
    string description;
};

struct struct_unit {
    string name;
    string surname;
    int age;
    int hunger;
    int happines;
    vector<struct_gene> *has_genes[12];
    struct_unit *maried_to;
    struct_unit *father;
    struct_unit *mother;
};

int main () {
    const int default_unit_count = 4;
    fstream gene_list;
    struct_gene * t_struct_gene;
    t_struct_gene = new struct_gene;
    gene_list.open("gene_list", ios::in);
    vector<struct_gene> vector_gene;
    //string name, description;

    gene_list >> t_struct_gene->name;
    getline(gene_list, t_struct_gene->description);
    while (gene_list) {
        vector_gene.push_back(*t_struct_gene);
        gene_list >> t_struct_gene->name;
        getline(gene_list, t_struct_gene->description);
    }
    delete t_struct_gene;

//   for (int i =0; i<vector_gene.size(); i++) {
//      cout <<vector_gene[i].name <<" "<<vector_gene[i].description<<endl;
//   }
    struct_unit *t_struct_unit;
    t_struct_unit = new struct_unit;

    srand ( time(NULL) );

    vector<struct_unit> vector_units;

    for (int i=0; i<default_unit_count; i++) {

        t_struct_unit->name = "unit_name";
        t_struct_unit->surname = "unit_surname";
        t_struct_unit->age = rand()%200;
        t_struct_unit->hunger = rand()%70 +30;
        t_struct_unit->happines = rand()%70 + 30;
        for (int i=0; i<12; i++) {
            t_struct_unit->has_genes[i] = vector_gene[rand()%vector_gene.size()];
        }
        t_struct_unit->maried_to = NULL;
        t_struct_unit->father = NULL;
        t_struct_unit->mother = NULL;
        vector_units.push_back(*t_struct_unit);
    }

    for (int i=0; i<vector_units.size(); i++) {
        cout << vector_units[i].name <<" "<<vector_units[i].surname<<endl;
        cout <<"Age:"<< vector_units[i].age <<" Hunger:"<<vector_units[i].hunger<<" Happines:"<<vector_units[i].happines<<endl;
        for (int j=0; j<0; j++) {
            cout <<vector_units[i].has_genes[j].name;
        }
        cout <<"==="<<endl;
    }
    delete t_struct_unit;
}

1 Comment

line 62 is where i am trying to write pointer to stuct_gene vector

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.