object to vector

Jun 7, 2013 at 10:11am
Hello people,

I am new in C++ and I have some problems with my code hopefully one of you can help me out.

I have a class PopulationFrequency which constructs an array of doubles.

I want to place and store this array N times into a vector in another class called Test.

I tried to use the push.back function but does not work could someone please help?

Looking forward to your reply.

Joez
Last edited on Jun 7, 2013 at 11:09am
Jun 7, 2013 at 11:25am
post your code.
Jun 7, 2013 at 11:29am
// Class Population frequency which constructs an array of doubles
class PopulationFrequency
{
protected:
short loci;
double* genotypefrequency;

private:
int arraysize;

public:
PopulationFrequency(int, int);
void setGenotypeFrequency(double newgenotypefrequency[]);
};

// Class evolution which should construct an array of PopulationFrequency
class Evolution
{
protected:

private:
int m_rows;


public:
Evolution(int);

};

Evolution::Evolution(int nrow)
{
m_rows = nrow * nrow;

vector<PopulationFrequency> objarray(m_rows, PopulationFrequency(3,10));

}
Jun 7, 2013 at 11:35am
you haven't mentioned how you have defined the constructor for PopulationFrequency(int,int).
Jun 7, 2013 at 11:37am
I am terribly sorry!

PopulationFrequency::PopulationFrequency(int setloci, int nindividuals)
{
loci = setloci;
arraysize = (int)pow(3, (float)loci);
genotypefrequency = new double[arraysize];
genotypefrequency[0] = 1;

for(int i =1; i < arraysize; i++)
{
genotypefrequency[i] = 0;
//cout << i;
}
}
Jun 7, 2013 at 2:30pm
Firstly, I have doubts about your function pow. I don't know how and where you have defined it. Is this the <cmath> built-in function that you are using?

Assuming it is correct, the other thing that is confusing me is that all the member variables of your PopulationFrequency class are either protected or private. This means they are not accessible from your Evolution class. Why would you want such an arrangment?

Lastly, since you are dealing with pointers and dynamic memory allocation, you will have to be careful with the different constructors (especially copy and assignment) and destructor of class PopulationFrequency to avoid unexpected crashes.
Jun 7, 2013 at 2:38pm
Yes I am using the <cmath> built-in function.

My idea is so generate (construct) in my classe evolution a vector or an array containing in each "cell" the object PopulationFrequency.

I can do that using:
vector<PopulationFrequency> objarray(m_rows, PopulationFrequency(3,10));

However this will be destroyed because it was and can not defined in the class so after the run it is deleted.
Jun 7, 2013 at 2:42pm
what problem / error are you getting?
Jun 7, 2013 at 2:44pm
That he does not create something because objarray is delete after the constructor in evolution is done.
Jun 7, 2013 at 2:51pm
Ok, seemingly the problem is because the vector objarray is a local variable of Evolution Constructor. It gets deleted once the code exits from this constructor. The vector should be declared in the Evolution class definition along with your other protected/private variables.

However, I am repeating, the objarray vector will not be of any use even then, because all the variables of PopulationFrequency are either protected or private and are inaccessible from the class Evolution.
Jun 7, 2013 at 3:08pm
Yes I tried to define it in private but then this does not work again:

vector<PopulationFrequency> objarray(N_times_PopulationFrequency, PopulationFrequency(loci, nindividuals));

Any suggestions?
Jun 7, 2013 at 3:18pm
Are you declaring it in the Evolution class? Then what are N_times_PopulationFrequency, loci and nindividuals? loci is a member of PopulationFrequency and not Evolution.

Try declaring your Evolution class like the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Evolution 
{
  protected:

  private:
    int m_rows;
    std::vector<PopulationFrequency> objarray;

  public:
    Evolution(int);
};

Evolution::Evolution(int nrow) 
{
  m_rows = nrow * nrow;
  for (int i=0; i<m_rows; i++)
    objarray.push_back( PopulationFrequency(3,10) );
}
Jun 10, 2013 at 8:19am
Thank you for your help abhishekm71 but I tried your idea but nothing gets pushed in objarray it is really strange!
Jun 10, 2013 at 10:39am
How do you know nothing gets pushed? What have you tried to output? Please remember, at present your code is such that even if you manage to create the array objarray, you will not be able to access its elements' member variables as all the variables have been declared as protected or private.

If you are certain that the elements are not being pushed, you can try doing:
1
2
3
4
5
6
7
8
Evolution::Evolution(int nrow) 
{
  m_rows = nrow * nrow;
  for (int i=0; i<m_rows; i++) {
    PopulationFrequency newElement(3,10);
    objarray.push_back( newElement );
  }
}
Last edited on Jun 10, 2013 at 10:40am
Topic archived. No new replies allowed.